Aion

 view release on metacpan or  search on metacpan

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

	init => [sub {
		@{$Aion::Type::SELF}{qw/min max/} = @{$Aion::Type::SELF->{args}};
	}],
	test => sub { $Aion::Type::SELF->{min} <= $_ && $_ <= $Aion::Type::SELF->{max} },
);

$Range->init;

3 ~~ $Range  # -> 1
4 ~~ $Range  # -> 1
5 ~~ $Range  # -> 1

2 ~~ $Range  # -> ""
6 ~~ $Range  # -> ""
```


## include ($element)

Проверяет, принадлежит ли аргумент классу.

```perl
my $PositiveInt = Aion::Type->new(
	name => "PositiveInt",
	test => sub { /^\d+$/ },
);

$PositiveInt->include(5) # -> 1
$PositiveInt->include(-6) # -> ""
```

## exclude ($element)

Проверяет, что аргумент не принадлежит классу.

```perl
my $PositiveInt = Aion::Type->new(
	name => "PositiveInt",
	test => sub { /^\d+$/ },
);

$PositiveInt->exclude(5)  # -> ""
$PositiveInt->exclude(-6) # -> 1
```

## coerce ($value)

Привести `$value` к типу, если приведение из типа и функции находится в `$self->{coerce}`.

Соответствует оператору `>>`.

```perl
my $Int = Aion::Type->new(name => "Int", test => sub { /^-?\d+\z/ });
my $Num = Aion::Type->new(name => "Num", test => sub { /^-?\d+(\.\d+)?\z/ });
my $Bool = Aion::Type->new(name => "Bool", test => sub { /^(1|0|)\z/ });

push @{$Int->{coerce}}, [$Bool, sub { 0+$_ }];
push @{$Int->{coerce}}, [$Num, sub { int($_+.5) }];

$Int->coerce(5.5)	 # => 6
$Int->coerce(undef)  # => 0
$Int->coerce("abc")  # => abc
```

## detail ($element, $feature)

Формирует сообщение ошибки.

```perl
my $Int = Aion::Type->new(name => "Int");

$Int->detail(-5, "Feature car") # => Feature car must have the type Int. The it is -5!

my $Num = Aion::Type->new(name => "Num", message => sub {
	"Error: $_ is'nt $Aion::Type::SELF->{property}!"
});

$Num->detail("x", "car") # => Error: x is'nt car!
```

## validate ($element, $feature)

Проверяет `$element` и выбрасывает сообщение `detail`, если элемент не принадлежит классу.

```perl
my $PositiveInt = Aion::Type->new(
	name => "PositiveInt",
	test => sub { /^\d+$/ },
);

eval {
	$PositiveInt->validate(-1, "Neg")
};
$@ # ~> Neg must have the type PositiveInt. The it is -1
```

## val_to_str ($val)

Переводит `$val` в строку.

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

## instanceof ($type)

Определяет, что тип является подтипом другого `$type` по имени типа.

В `|` и `~` не заходит. Аргументы не проверяет.

```perl
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
```

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


Это - примитивный тип, то есть тот, в иерархии которого нет множественно-теоритических операторов.

```perl
Aion::Types::Int->is_primitive  # -> 1
Aion::Types::Like->is_primitive # -> ""
```

## is_union ()

Это объединение типов.

```perl
Aion::Types::Int->is_union # -> ""
(Aion::Types::Int | Aion::Types::Int)->is_union  # -> 1
```

## is_intersection ()

Это пересечение типов.

```perl
Aion::Types::Int->is_intersection # -> ""
(Aion::Types::Int & Aion::Types::Int)->is_intersection  # -> 1
```

## is_exclude ()

Это исключение типа.

```perl
Aion::Types::Any->is_exclude # -> ""
(~Aion::Types::Any)->is_exclude # -> 1
Aion::Types::None->is_exclude # -> 1
~Aion::Types::Any eq Aion::Types::None # -> 1
```

## is_enum ()

Это перечисление.

```perl
Aion::Types::Int->is_enum  # -> ""
Aion::Types::Enum([1])->is_enum  # -> 1
```

## is_range_type ()

Это интервальный тип.

```perl
Aion::Types::Int->is_range_type  # -> ""
Aion::Types::Len([10])->is_range_type  # -> 1
```

## range_lbound ()

Нижняя граница интервала.

```perl
Aion::Types::Int->range_lbound  # -> undef
Aion::Types::Len([10])->range_lbound  # -> 0
Aion::Types::Range([0, 10])->range_lbound  # -> '-Inf'
```

## is_range ()

Это интервал.

```perl
Aion::Types::Int->is_range  # -> ""
Aion::Types::Len([10])->is_range  # -> ""
Aion::Types::Range([1, 10])->is_range  # -> 1
```

## typed_sorted_args_key ()

Формирует ключ с отсортированными типизированными параметрами.

```perl
(Aion::Types::Int & Aion::Types::Num)->typed_sorted_args_key  # -> (Aion::Types::Num & Aion::Types::Int)->typed_sorted_args_key
```

## sorted_args_key ()

Формирует ключ с отсортированными нетипизированными параметрами.

```perl
Aion::Types::Enum([10, 20])->sorted_args_key # -> Aion::Types::Enum([20, 10])->sorted_args_key
```

## key ()

Уникальный ключ из прототипа типа и его параметров.

## keyfn ($fn)

Устанавливает/возвращает функцию построения ключа для типа как класса.

```perl
my $type = Aion::Type->new(name => 'New', args => [10, 20]);
$type->keyfn($type->can('sorted_args_key'));

my $type2 = Aion::Type->new(name => 'New', args => [20, 10], coerce => $type->{coerce});
$type->key # -> $type2->key
```

## asen ()

Возвращает цепочку предков.

```perl
[Aion::Types::Num->asen]  # --> [Aion::Types::Any, Aion::Types::Item, Aion::Types::Defined, Aion::Types::Value, Aion::Types::Str]
```

## ckey ()

Ключ для сравнения типов в <=> и cmp.

## compare ($other)



( run in 1.142 second using v1.01-cache-2.11-cpan-d80b1682f3f )