Aion

 view release on metacpan or  search on metacpan

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

=head2 val_to_str ($val)

Converts C<$val> to a string.

	Aion::Type->new->val_to_str([1,2,{x=>6}]) # => [1, 2, {x => 6}]

=head2 instanceof ($type)

Determines that a type is a subtype of another C<$type> by type name.

Doesn't work in C<|> and C<~>. Doesn't check arguments.

	my $Int = Aion::Type->new(name => "Int");
	my $PositiveInt = Aion::Type->new(name => "PositiveInt", as => $Int);
	
	$PositiveInt->instanceof('Int');          # -> 1
	$PositiveInt->instanceof('PositiveInt');  # -> 1
	$Int->instanceof('PositiveInt');          # -> ""
	
	my $MyEnum = Aion::Type->new(name => "MyEnum", args => [3, 5, 'car']);
	($MyEnum & $PositiveInt)->instanceof('Int'); # -> 1

=head2 is_set_theoretic

Checks that the type is set-theoretic (ie - the C<|>, C<&> or C<~> operator).

=head2 simplify

If the expression has no values, it will return C<~Any>, otherwise it will return the expression.

Simplification of the expression in this function may appear in the future.

	package Aion::Types;
	
	my $type = (Enum[1,2] | Enum[2,3]) & Enum[2,3,4];
	
	$type->simplify->stringify # => ( ( Enum[1, 2] | Enum[2, 3] ) & Enum[2, 3, 4] )
	
	my $range = Range[-10,0] & Range[4,8];
	$range->simplify->stringify # => ~Any

=head2 Any

A constant for a type that includes all values.

	package Aion::Type;
	
	42 ~~ Any   # -> 1
	42 ~~ None  # -> ""
	
	Any <= Any   # -> 1
	None <= Any  # -> 1
	Any <= None  # -> ""

=head2 None

Constant for an empty type that does not contain anything.

=head2 identical ($type)

Types are equal if they have the same prototype (C<coerce>), the same number of arguments, parent element, their arguments, and M and N are equal.

	my $Int = Aion::Type->new(name => "Int");
	my $PositiveInt = Aion::Type->new(name => "PositiveInt", as => $Int);
	my $AnotherInt = Aion::Type->new(name => "Int", coerce => $Int->{coerce});
	my $IntWithArgs = Aion::Type->new(name => "Int", args => [1, 2]);
	my $AnotherIntWithArgs = Aion::Type->new(name => "Int", args => [1, 2], coerce => $IntWithArgs->{coerce});
	my $IntWithDifferentArgs = Aion::Type->new(name => "Int", args => [3, 4]);
	my $Str = Aion::Type->new(name => "Str");
	
	$Int->identical($Int)                        # -> 1
	$Int->identical($AnotherInt)                 # -> 1
	$IntWithArgs->identical($AnotherIntWithArgs) # -> 1
	$PositiveInt->identical($PositiveInt)        # -> 1
	
	$Int->{coerce} == $Str->{coerce}               # -> ""
	$Int->identical($Str)                          # -> ""
	$Int->identical($IntWithArgs)                  # -> ""
	$IntWithArgs->identical($IntWithDifferentArgs) # -> ""
	$PositiveInt->identical($Int)                  # -> ""
	
	$Int->identical("not a type") # -> ""
	
	my $PositiveInt2 = Aion::Type->new(name => "PositiveInt", as => $Str);
	$PositiveInt->identical($PositiveInt2) # -> ""
	
	$Int->identical($PositiveInt) # -> ""
	$PositiveInt->identical($Int) # -> ""
	
	my $PositiveIntWithArgs = Aion::Type->new(name => "PositiveInt", as => $Int, args => [1]);
	my $PositiveIntWithArgs2 = Aion::Type->new(name => "PositiveInt", as => $Int, args => [2]);
	$PositiveIntWithArgs->identical($PositiveIntWithArgs2) # -> ""

=head2 distinct ($type)

Reverse operation to C<identical>.

	my $Int = Aion::Type->new(name => "Int");
	my $PositiveInt = Aion::Type->new(name => "PositiveInt", as => $Int);
	
	$Int->distinct($PositiveInt) # -> 1
	$Int ne $PositiveInt         # -> 1

=head2 disjoint ($other)

A type does not overlap with another type.

=head2 subset ($type)

Specifies that it is a subset of the specified type.

=head2 superset ($type)

Specifies that it is a superset of the specified type.

=head2 subproper ($other)

A type is a strict subset of another.

=head2 superproper ($other)



( run in 0.556 second using v1.01-cache-2.11-cpan-817d5f8af8b )