The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Function::Interface - declare typed interface package

SYNOPSIS

Declare typed interface package IFoo:

    package IFoo {
        use Function::Interface;
        use Types::Standard -types;

        fun hello(Str $msg) :Return(Str);

        fun add(Int $a, Int $b) :Return(Int);
    }

Implements the interface package IFoo:

    package Foo {
        use Function::Interface::Impl qw(IFoo);
        use Types::Standard -types;

        fun hello(Str $msg) :Return(Str) {
            return "HELLO $msg";
        }

        fun add(Int $a, Int $b) :Return(Int) {
            return $a + $b;
        }
    }

Use the type ImplOf:

    package FooService {
        use Function::Interface::Types qw(ImplOf);
        use Function::Parameters;
        use Function::Return;
        use Mouse;

        use aliased 'IFoo';

        fun greet(ImplOf[IFoo] $foo) :Return() {
            print $foo->hello;
            return;
        }
    }

    my $foo_service = FooService->new;
    my $foo = Foo->new; # implements of IFoo

    $foo_service->greet($foo);

DESCRIPTION

This module provides a typed interface. Function::Interface declares abstract functions without implementation and defines an interface package. Function::Interface::Impl checks if the abstract functions are implemented at compile time.

SUPPORT

This module supports all perl versions starting from v5.14.

Declare function

Function::Interface provides two new keywords, fun and method, for declaring abstract functions and methods with types:

    fun hello(Str $msg) :Return(Str);

    method new(Num :$x, Num :$y) :Return(Point);

The method of declaring abstract functions is the same as Function::Parameters and Function::Return.

declare parameters

Function arguments must always specify a variable name and type constraint, and named arguments and optional arguments can optionally be specified:

    # positional parameters
    # e.g. called `foo(1,2,3)`
    fun foo1(Int $a, Int $b, Int $c) :Return();

    # named parameters
    # e.g. called `bar(x => 123, y => 456)`
    fun foo2(Num :$x, Num :$y) :Return();

    # optional
    # e.g. called `baz()` or `baz('some')`
    fun foo3(Str $msg=) :Return();

declare return types

Specify zero or more type constraints for the function's return value:

    # zero(empty)
    fun bar1() :Return();

    # one
    fun bar2() :Return(Str);

    # two
    fun bar3() :Return(Str, Num);

requirements of type constraint

The requirements of type constraint of Function::Interface is the same as for Function::Parameters and Function::Return.

METHODS

Function::Interface::info($interface_package)

The function Function::Interface::info lets you introspect interface functions:

    # declare interface package
    package IBar {
        use Function::Interface;
        fun hello() :Return();
        fun world() :Return();
    }

    # introspect
    my $info = Function::Interface::info 'IBar';
    $info->package; # => IBar
    $info->functions; # => list of Function::Interface::Info::Function

It returns either undef if it knows nothing about the interface or an object of Function::Interface::Info.

SEE ALSO

Function::Interface::Impl

LICENSE

Copyright (C) kfly8.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

kfly8 <kfly@cpan.org>