Types::Sub - type constraints for subroutines and Sub::Meta
use Test2::V0; use Types::Sub -types; use Types::Standard -types; my $Sub = Sub[ args => [Int, Int], returns => Int ]; use Function::Parameters; use Function::Return; fun add(Int $a, Int $b) :Return(Int) { return $a + $b } ok $Sub->check(\&add); ok !$Sub->check(sub {}); done_testing;
Types::Sub is type library for subroutines and Sub::Meta. This library can be used with Moo/Moose/Mouse, etc.
Types::Sub
A value where Ref['CODE'] and check by Sub::Meta#is_relaxed_same_interface.
Ref['CODE']
Sub::Meta#is_relaxed_same_interface
use Types::Sub -types; use Types::Standard -types; use Sub::Meta; use Function::Parameters; use Function::Return; fun distance(Num :$lat, Num :$lng) :Return(Num) { } # # Sub[`a] is ... # my $Sub = Sub[ subname => 'distance', args => { '$lat' => Num, '$lng' => Num }, returns => Num ]; ok $Sub->check(\&distance); # # almost equivalent to the following # my $submeta = Sub::Meta->new( subname => 'distance', args => { '$lat' => Num, '$lng' => Num }, returns => Num ); my $meta = Sub::Meta::CreatorFunction::find_submeta(\&distance); ok $submeta->is_relaxed_same_interface($meta); done_testing;
If no argument is given, it matches Ref['CODE']. Sub[] == Ref['CODE']. This helps to keep writing simple when choosing whether or not to use stricter type checking depending on the environment.
Sub[] == Ref['CODE']
use Devel::StrictMode; has callback => ( is => 'ro', isa => STRICT ? Sub[ args => [Int], returns => [Int], ] : Sub[] );
A value where Ref['CODE'] and check by Sub::Meta#is_strict_same_interface.
Sub::Meta#is_strict_same_interface
A value where checking by Sub::Meta#is_relaxed_same_interface.
A value where checking by Sub::Meta#is_strict_same_interface.
use Function::Parameters; use Types::Standard -types; use Types::Sub -types; my $Sub = Sub[ args => [Int, Int], ]; fun add(Int $a, Int $b) { return $a + $b } fun double(Int $a) { return $a * 2 } ok $Sub->check(\&add); ok !$Sub->check(\&double);
use Sub::WrapInType; use Types::Standard -types; use Types::Sub -types; my $Sub = Sub[ args => [Int, Int], returns => Int, ]; ok $Sub->check(wrap_sub([Int,Int] => Int, sub {})); ok !$Sub->check(wrap_sub([Int] => Int, sub {}));
use Sub::WrapInType::Attribute; use Types::Standard -types; use Types::Sub -types; my $Sub = Sub[ args => [Int, Int], returns => Int, ]; sub add :WrapSub([Int,Int] => Int) { my ($a, $b) = @_; return $a + $b } sub double :WrapSub([Int] => Int) { my $a = shift; return $a * 2 } ok $Sub->check(\&add); ok !$Sub->check(\&double);
use Sub::Meta::Library; use Types::Standard -types; use Types::Sub -types; my $Sub = Sub[ args => [Int, Int], returns => Int, ]; sub add {} my $meta = Sub::Meta->new( args => [Int, Int], returns => Int, ); Sub::Meta::Library->register(\&add, $meta); ok $Sub->check(\&add);
Sub::Meta::Type
Copyright (C) kfly8.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
kfly8 <kfly@cpan.org>
To install Sub::Meta, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sub::Meta
CPAN shell
perl -MCPAN -e shell install Sub::Meta
For more information on module installation, please visit the detailed CPAN module installation guide.