Aion

 view release on metacpan or  search on metacpan

lib/Aion/Types.pm  view on Meta::CPAN


=head2 via ($code)

Syntactic sugar for C<coerce>.

=head1 ATTRIBUTES

=head2 :Isa (@signature)

Checks the signature of a subroutine: arguments and results.

	sub minint($$) : Isa(Int => Int => Int) {
		my ($x, $y) = @_;
		$x < $y? $x : $y
	}
	
	minint 6, 5; # -> 5
	eval {minint 5.5, 2}; $@ # ~> Arguments of method `minint` must have the type Tuple\[Int, Int\]\.
	
	sub half($) : Isa(Int => Int) {
		my ($x) = @_;
		$x / 2
	}
	
	half 4; # -> 2
	eval {half 5}; $@ # ~> Return of method `half` must have the type Int. The it is 2.5

=head1 TYPES

=head2 Any

The top level type in the hierarchy. Compares everything.

=head2 Control

The top-level type in hierarchy constructors creates new types from any types.

=head2 Union[A, B...]

Union of several types. Similar to the C<$type1 | $type2>.

	33  ~~ Union[Int, Ref] # -> 1
	[]  ~~ Union[Int, Ref]	# -> 1
	"a" ~~ Union[Int, Ref]	# -> ""

=head2 Intersection[A, B...]

The intersection of several types. Similar to the C<$type1 & $type2> operator.

	15 ~~ Intersection[Int, StrMatch[/5/]] # -> 1

=head2 Exclude[A, B...]

Exclusion of several types. Similar to the C<~$type> operator.

	-5  ~~ Exclude[PositiveInt] # -> 1
	"a" ~~ Exclude[PositiveInt] # -> 1
	5   ~~ Exclude[PositiveInt] # -> ""
	5.5 ~~ Exclude[PositiveInt] # -> 1

If C<Exclude> has many arguments, then it is analogous to C<~ ($type1 | $type2 ...)>.

	-5  ~~ Exclude[PositiveInt, Enum[-2]] # -> 1
	-2  ~~ Exclude[PositiveInt, Enum[-2]] # -> ""
	0   ~~ Exclude[PositiveInt, Enum[-2]] # -> ""

=head2 Option[A]

Additional keys in C<Dict>.

	{a=>55} ~~ Dict[a=>Int, b => Option[Int]]          # -> 1
	{a=>55, b=>31} ~~ Dict[a=>Int, b => Option[Int]]   # -> 1
	{a=>55, b=>31.5} ~~ Dict[a=>Int, b => Option[Int]] # -> ""

=head2 Wantarray[A, S]

If the routine returns different values in array and scalar contexts, then the C<Wantarray> type is used with type C<A> for the array context and type C<S> for the scalar context.

	sub arr : Isa(PositiveInt => Wantarray[ArrayRef[PositiveInt], PositiveInt]) {
		my ($n) = @_;
		wantarray? 1 .. $n: $n
	}
	
	my @a = arr(3);
	my $s = arr(3);
	
	\@a # --> [1,2,3]
	$s  # -> 3

=head2 Item

The top-level type in the hierarchy of scalar types.

=head2 External[type]

Sets C<type> to C<Aion::Type>.

=over

=item * If C<type> is C<Aion::Type>, then returns it unchanged.

=item * If C<type> is a string, then wraps it in C<Object>.

=item * If C<type> can be called, then wraps it in C<< Aion::Type-E<gt>new(test =E<gt> $type, ...) >>. And if it has a C<coerce> method, it will use it for transformations. Thanks to this, it is possible to use external types like C<Type::Tiny> in th...

=back

	External['Aion'] # -> Object['Aion']
	External[sub { /^x/ }] ~~ 'xyz' # -> 1
	
	package MyInt {
		use overload "&{}" => sub {
			sub { /^[+-]?[0-9]+$/ }
		};
		
		sub coerce { /\./? int($_): $_ }
	}
	
	my $myint = bless {}, 'MyInt';
	
	External([$myint]) ~~ '+123' # -> 1



( run in 1.165 second using v1.01-cache-2.11-cpan-98e64b0badf )