The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Type::Utils - utility functions to make defining type constraints a little easier

SYNOPSIS

   package Types::Mine;
   
   use Type::Library -base;
   use Type::Utils;
   
   extends "Types::Standard";
   
   declare "AllCaps",
      as "Str",
      where { uc($_) eq $_ },
      inline_as { my $varname = $_[1]; "uc($varname) eq $varname" };
   
   coerce "AllCaps",
      from "Str", via { uc($_) };

DESCRIPTION

This module provides utility functions to make defining type constraints a little easier.

Moose::Util::TypeConstraints-like

The following are similar to the similarly named functions described in Moose::Util::TypeConstraints.

subtype $name, %options
subtype %options
type $name, %options
type %options
as $parent
where { BLOCK }
message { BLOCK }
inline_as { BLOCK }
class_type $name, { class => $package, %options }
class_type { class => $package, %options }
role_type $name, { role => $package, %options }
role_type { role => $package, %options }
duck_type $name, \@methods
duck_type \@methods
union $name, \@constraints
union \@constraints
enum $name, \@values
enum \@values
coerce $target, @coercions
from $source
via { BLOCK }

Other

declare $name, %options
declare %options

declare is a function which works like subtype and type. In fact, the latter pair are just aliases for the former.

If the caller package inherits from Type::Library then any non-anonymous types declared in the package will be automatically installed into the library.

intersection $name, \@constraints
intersection \@constraints

Defines a type constraint which is the intersection of several existing constraints.

extends @library

Indicates that this type library extends other type libraries, importing their type constraints.

declare_coercion $name, \%opts, $type1, $code1, ...
declare_coercion \%opts, $type1, $code1, ...

Declares a coercion that is not explicitly attached to any type in the library. For example:

   declare_coercion "ArrayRefFromAny", from "Any", via { [$_] };

This coercion will be exportable from the library as a Type::Coercion object, but the ArrayRef type exported by the library won't automatically use it.

Coercions declared this way are immutable (frozen).

to_type $type

Used with declare_coercion to declare the target type constraint for a coercion, but still without explicitly attaching the coercion to the type constraint:

   declare_coercion "ArrayRefFromAny",
      to_type "ArrayRef",
      from "Any", via { [$_] };

You should pretty much always use this when declaring an unattached coercion because it's exceedingly useful for a type coercion to know what it will coerce to - this allows it to skip coercion when no coercion is needed (e.g. avoiding coercing [] to [ [] ]) and allows assert_coerce to work properly.

EXPORT

By default, all of the functions documented above are exported, except subtype and type (prefer declare instead).

This module uses Exporter::TypeTiny; see the documentation of that module for tips and tricks importing from Type::Utils.

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny.

SEE ALSO

Type::Tiny::Manual.

Type::Tiny, Type::Library, Types::Standard, Type::Coercion.

Type::Tiny::Class, Type::Tiny::Role, Type::Tiny::Duck, Type::Tiny::Enum, Type::Tiny::Union.

Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2013 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.