Aion

 view release on metacpan or  search on metacpan

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

	
	2.5 ~~ Seria[1,2,3,4] # -> 1

Used in C<init_where>, C<where> and C<awhere>.

=head2 M, N

C<M> and C<N> are shorthand for C<< SELF-E<gt>{M} >> and C<< SELF-E<gt>{N} >>.

	BEGIN {
		subtype "BeginAndEnd[begin, end]",
			init_where {
				N = qr/^${\ quotemeta A}/;
				M = qr/${\ quotemeta B}$/;
			}
			where { $_ =~ N && $_ =~ M };
	}
	
	"Hi, my dear!" ~~ BeginAndEnd["Hi,", "!"]; # -> 1
	"Hi my dear!" ~~ BeginAndEnd["Hi,", "!"];  # -> ""
	
	"" . BeginAndEnd["Hi,", "!"] # => BeginAndEnd['Hi,', '!']

=head2 message ($code)

Used with C<subtype> to print an error message if the value excludes the type. C<$code> uses: C<SELF> - the current type, C<ARGS>, C<A>, C<B>, C<C>, C<D> - type arguments (if any) and a test value in C<$_>. It can be converted to a string using C<< S...

=head2 coerce ($type, from => $from, via => $via)

Adds a new cast (C<$via>) to C<$type> from C<$from> type.

	BEGIN {
		subtype Four => where {4 eq $_};
	}
	
	"4a" ~~ Four # -> ""
	
	Four->coerce("4a") # -> "4a"
	
	coerce Four, from Str, via { 0+$_ };
	
	Four->coerce("4a")	# -> 4
	
	coerce Four, from ArrayRef, via { scalar @$_ };
	
	Four->coerce([1,2,3])           # -> 3
	Four->coerce([1,2,3]) ~~ Four   # -> ""
	Four->coerce([1,2,3,4]) ~~ Four # -> 1

Can use parameters like:

	BEGIN {
		subtype 'Plus[acc]', as Num;
	}
	coerce &Plus, from Num, via { $_ + A };
	
	Plus([5])->coerce(4) # -> 9
	Plus([5]) >> 5 # -> 10
	1 >> Plus[5] # -> 6

C<coerce> throws exceptions:

	eval {coerce Int, via1 => 1}; $@  # ~> coerce Int unused keys left: via1
	eval {coerce "x"}; $@  # ~> coerce x not Aion::Type!
	eval {coerce Int}; $@  # ~> coerce Int: from is'nt Aion::Type!
	eval {coerce Int, from "x"}; $@  # ~> coerce Int: from is'nt Aion::Type!
	eval {coerce Int, from Num}; $@  # ~> coerce Int: via is not subroutine!
	eval {coerce Int, (from=>Num, via=>"x")}; $@  # ~> coerce Int: via is not subroutine!

Standard casts:

	# Str from Undef — empty string
	Str->coerce(undef) # -> ""
	
	# Int from Num — rounded integer
	Int->coerce(2.5)  # -> 3
	Int->coerce(-2.5) # -> -3
	
	# Bool from Any — 1 or ""
	Bool->coerce([]) # -> 1
	Bool->coerce(0)  # -> ""

=head2 from ($type)

Syntactic sugar for C<coerce>.

=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.



( run in 0.904 second using v1.01-cache-2.11-cpan-f56aa216473 )