NAME
Type::Alias - type alias for type constraints
SYNOPSIS
use
Types::Standard -types;
type
ID
=> Str;
type
User
=> {
id
=> ID,
name
=> Str,
age
=> Int,
};
type
List
=>
sub
{
my
(
$R
) =
@_
;
$R
? ArrayRef[
$R
] : ArrayRef;
};
type
UserData
=> List[User] | User;
UserData->check([
{
id
=>
'1'
,
name
=>
'foo'
,
age
=> 20 },
{
id
=>
'2'
,
name
=>
'bar'
,
age
=> 30 },
]);
# OK
UserData->check(
{
id
=>
'1'
,
name
=>
'foo'
,
age
=> 20 },
);
# OK
# Internally List[User] is equivalent to the following type:
#
# ArrayRef[
# Dict[
# age=>Int,
# id=>Str,
# name=>Str
# ]
# ]
DESCRIPTION
Type::Alias creates type aliases and type functions for existing type constraints such as Type::Tiny, Moose, Mouse. The aim of this module is to enhance the reusability of types and make it easier to express types.
IMPORT OPTIONS
-alias
-alias
is an array reference that defines type aliases. The default is []
.
type
ID
=> Str;
type
User
=> {
id
=> ID,
name
=> Str,
age
=> Int,
};
-fun
-fun
is an array reference that defines type functions. The default is []
.
type
The type
option is used to configure the type function that defines type aliases and type functions.
# Rename type function:
mytype
ID
=> Str;
# declare type alias
EXPORTED FUNCTIONS
type($alias_name, $type_args)
type
is a function that defines type alias and type function.
It recursively generates type constraints based on $type_args
.
Given a type constraint in $type_args
, it returns the type constraint as is.
Type::Alias treats objects with check
and get_message
methods as type constraints.
type
ID
=> Str;
ID->check(
'foo'
);
# OK
Internally ID
is equivalent to the following type:
sub
ID() { Str }
Given a hash reference in $type_args
, it returns the type constraint defined by Type::Tiny's Dict type.
type
Point
=> {
x
=> Int,
y
=> Int,
};
Point->check({
x
=> 1,
y
=> 2
});
# OK
Internally Point
is equivalent to the following type:
sub
Point() { Dict[
x
=>Int,
y
=>Int] }
Given an array reference in $type_args
, it returns the type constraint defined by Type::Tiny's Tuple type.
type
Option
=> [Str, Int];
Option->check(
'foo'
, 1);
# OK
Internally Option
is equivalent to the following type:
sub
Option() { Tuple[Str,Int] }
Given a code reference in $type_args
, it defines a type function that accepts a type constraint as an argument and return the type constraint.
type
List
=>
sub
(
$R
) {
$R
? ArrayRef[
$R
] : ArrayRef;
};
type
Points
=> List[{
x
=> Int,
y
=> Int }];
Points->check([
{
x
=> 1,
y
=> 2 },
{
x
=> 3,
y
=> 4 },
]);
# OK
Internally List
is equivalent to the following type:
sub
List :
prototype
(;$) {
my
@args
=
map
{ Type::Alias::to_type(
$_
) } @{
$_
[0]};
sub
(
$R
) {
$R
? ArrayRef[
$R
] : ArrayRef;
}->(
@args
);
}
And Points
is equivalent to the following type:
sub
Points() { List[Dict[
x
=>Int,
y
=>Int]] }
COOKBOOK
Exporter
Type::Alias is designed to be used with Exporter. The following is an example of using Type::Alias with Exporter.
Class builders
Type::Alias is designed to be used with class builders such as Moose, Moo and Mouse.
Validation modules
Type::Alias is designed to be used with validation modules such as Type::Params, Smart::Args::TypeTiny and Data::Validator:
use
Type::Params -sigs;
type
Message
=> Str &
sub
{
length
(
$_
) > 1 };
signature_for
hello
=> (
positional
=> [ Message ],
);
sub
hello {
my
(
$message
) =
@_
;
return
"HELLO "
.
$message
;
}
hello(
'World'
)
# => 'HELLO World';
hello(
''
)
# => Error!
NOTE
Function::Parameters works using type aliases from outside.
package
Sample {
our
@EXPORT_OK
=
qw(User)
;
type
User
=> {
name
=> Str,
};
}
use
Types::Standard -types;
use
Function::Parameters;
fun hello (User
$user
) {
return
"Hello, $user->{name}!"
;
}
hello({
name
=>
'foo'
})
# => 'Hello, foo!';
However, if you write a type alias inline as follows, the current implementation will not work.
type
Gorilla
=> Dict[
name
=> Str ];
fun ooh(Gorilla
$user
) {
# => ERROR: type Gorilla is not defined at compile time
return
"ooh ooh, $user->{name}!"
;
}
ooh({
name
=>
'gorilla'
})
# => 'ooh ooh, gorilla!';
SEE ALSO
LICENSE
Copyright (C) kobaken.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
kobaken kfly@cpan.org