NAME
Type::Alias - type alias for type constraints
SYNOPSIS
use Type::Alias -declare => [qw(ID User List)];
use Types::Standard -types;
type ID => Str;
type User => {
id => ID,
name => Str,
age => Int,
};
type List => sub($R) {
$R ? ArrayRef[$R] : ArrayRef;
};
# =>
# ArrayRef[
# Dict[
# age=>Int,
# id=>Str,
# name=>Str
# ]
# ]
DESCRIPTION
Type::Alias creates type aliases for existing type constraints such as Type::Tiny, Moose. The aim of this module is to enhance the reusability of types and make it easier to express types.
IMPORT OPTIONS
-declare
-declare
is an array reference that defines type aliases. The default is []
.
use Type::Alias -declare => [qw(ID User List)];
-type_alias
-type_alias
is a function name that defines type aliases. The default name is type.
use Type::Alias -type_alias => 'mytype';
mytype ID => Str; # declare type alias
-export_ok
-export_ok
is an array reference that defines type aliases to be exported. The default is all type aliases defined by -declare
.
# Default case:
use Type::Alias -declare => [qw(ID User List)];
our @EXPORT_OK; # => qw(ID User List);
# Specify export_ok:
use Type::Alias -declare => [qw(ID User List)], -export_ok => [qw(List)];
our @EXPORT_OK; # => qw(List);
EXPORTED FUNCTIONS
type($alias_name, $type_alias_args)
type
is a function that defines type aliases. The default name is type.
Given a type constraint in $type_alias_args
, it returns the type constraint as is. Type::Alias treats objects with check
and get_message
methods as type constraints.
type ID => Str;
# sub ID(;$) { Str }
Given a hash reference in $type_alias_args
, it returns the type constraint defined by Type::Tiny's Dict type.
type Point => {
x => Int,
y => Int,
};
# sub Point(;$) { Dict[x=>Int,y=>Int] }
Given an array reference in $type_alias_args
, it returns the type constraint defined by Type::Tiny's Tuple type.
type Option => [Str, Int];
# sub Option(;$) { Tuple[Str,Int] }
Given a code reference in $type_alias_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;
};
# sub List :prototype(;$) {
# my $R = Type::Alias::to_type($_[0]);
# $R ? ArrayRef[$R] : ArrayRef;
# }
Internally, it recursively generates Type::Tiny type constraints based on $type_alias_args
using the Type::Alias::to_type function.
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>