Toby Inkster > Type-Tiny > Type::Tiny

Download:
Type-Tiny-0.038.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  2
Stalled  1
View/Report Bugs
Module Version: 0.038   Source   Latest Release: Type-Tiny-0.043_02

NAME ^

Type::Tiny - tiny, yet Moo(se)-compatible type constraint

SYNOPSIS ^

   use Scalar::Util qw(looks_like_number);
   use Type::Tiny;
   
   my $NUM = "Type::Tiny"->new(
      name       => "Number",
      constraint => sub { looks_like_number($_) },
      message    => sub { "$_ ain't a number" },
   );
   
   package Ermintrude {
      use Moo;
      has favourite_number => (is => "ro", isa => $NUM);
   }
   
   package Bullwinkle {
      use Moose;
      has favourite_number => (is => "ro", isa => $NUM);
   }
   
   package Maisy {
      use Mouse;
      has favourite_number => (is => "ro", isa => $NUM);
   }

STATUS ^

This module is covered by the Type-Tiny stability policy.

DESCRIPTION ^

Type::Tiny is a tiny class for creating Moose-like type constraint objects which are compatible with Moo, Moose and Mouse.

Maybe now we won't need to have separate MooseX, MouseX and MooX versions of everything? We can but hope...

This documents the internals of Type::Tiny. Type::Tiny::Manual is a better starting place if you're new.

Constructor

new(%attributes)

Moose-style constructor function.

Attributes

name

The name of the type constraint. These need to conform to certain naming rules (they must begin with an uppercase letter and continue using only letters, digits 0-9 and underscores).

Optional; if not supplied will be an anonymous type constraint.

display_name

A name to display for the type constraint when stringified. These don't have to conform to any naming rules. Optional; a default name will be calculated from the name.

parent

Optional attribute; parent type constraint. For example, an "Integer" type constraint might have a parent "Number".

If provided, must be a Type::Tiny object.

constraint

Coderef to validate a value ($_) against the type constraint. The coderef will not be called unless the value is known to pass any parent type constraint.

Defaults to sub { 1 } - i.e. a coderef that passes all values.

compiled_check

Coderef to validate a value ($_[0]) against the type constraint. This coderef is expected to also handle all validation for the parent type constraints.

The general point of this attribute is that you should not set it, and rely on the lazily-built default. Type::Tiny will usually generate a pretty fast coderef.

message

Coderef that returns an error message when $_ does not validate against the type constraint. Optional (there's a vaguely sensible default.)

inlined

A coderef which returns a string of Perl code suitable for inlining this type. Optional.

If constraint (above) is a coderef generated via Sub::Quote, then Type::Tiny may be able to automatically generate inlined for you.

library

The package name of the type library this type is associated with. Optional. Informational only: setting this attribute does not install the type into the package.

coercion

A Type::Coercion object associated with this type.

Generally speaking this attribute should not be passed to the constructor; you should rely on the default lazily-built coercion object.

You may pass coercion => 1 to the constructor to inherit coercions from the constraint's parent. (This requires the parent constraint to have a coercion.)

complementary_type

A complementary type for this type. For example, the complementary type for an integer type would be all things that are not integers, including floating point numbers, but also alphabetic strings, arrayrefs, filehandles, etc.

Generally speaking this attribute should not be passed to the constructor; you should rely on the default lazily-built complementary type.

moose_type, mouse_type

Objects equivalent to this type constraint, but as a Moose::Meta::TypeConstraint or Mouse::Meta::TypeConstraint.

Generally speaking this attribute should not be passed to the constructor; you should rely on the default lazily-built objects.

It should rarely be necessary to obtain a Moose::Meta::TypeConstraint object from Type::Tiny because the Type::Tiny object itself should be usable pretty much anywhere a Moose::Meta::TypeConstraint is expected.

The following additional attributes are used for parameterizable (e.g. ArrayRef) and parameterized (e.g. ArrayRef[Int]) type constraints. Unlike Moose, these aren't handled by separate subclasses.

parameters

In parameterized types, returns an arrayref of the parameters.

name_generator

A coderef which generates a new display_name based on parameters. Optional; the default is reasonable.

constraint_generator

Coderef that generates a new constraint coderef based on parameters. Optional; providing a generator makes this type into a parameterizable type constraint.

inline_generator

A coderef which generates a new inlining coderef based on parameters.

coercion_generator

A coderef which generates a new Type::Coercion object based on parameters.

deep_explanation

This API is not finalized. Coderef used by Error::TypeTiny::Assertion to peek inside parameterized types and figure out why a value doesn't pass the constraint.

Methods

has_parent, has_library, has_inlined, has_constraint_generator, has_inline_generator, has_coercion_generator, has_parameters, has_message, has_deep_explanation

Predicate methods.

has_coercion

Predicate method with a little extra DWIM. Returns false if the coercion is a no-op.

is_anon

Returns true iff the type constraint does not have a name.

is_parameterized, is_parameterizable

Indicates whether a type has been parameterized (e.g. ArrayRef[Int]) or could potentially be (e.g. ArrayRef).

qualified_name

For non-anonymous type constraints that have a library, returns a qualified "Library::Type" sort of name. Otherwise, returns the same as name.

parents

Returns a list of all this type constraint's ancestor constraints. For example, if called on the Str type constraint would return the list (Value, Defined, Item, Any).

Due to a historical misunderstanding, this differs from the Moose implementation of the parents method. In Moose, parents only returns the immediate parent type constraints, and because type constraints only have one immediate parent, this is effectively an alias for parent. The extension module MooseX::Meta::TypeConstraint::Intersection is the only place where multiple type constraints are returned; and they are returned as an arrayref in violation of the base class' documentation. I'm keeping my behaviour as it seems more useful.

equals($other), is_subtype_of($other), is_supertype_of($other), is_a_type_of($other)

Compare two types. See Moose::Meta::TypeConstraint for what these all mean. (OK, Moose doesn't define is_supertype_of, but you get the idea, right?)

Note that these have a slightly DWIM side to them. If you create two Type::Tiny::Class objects which test the same class, they're considered equal. And:

   my $subtype_of_Num = Types::Standard::Num->create_child_type;
   my $subtype_of_Int = Types::Standard::Int->create_child_type;
   $subtype_of_Int->is_subtype_of( $subtype_of_Num );  # true
strictly_equals($other), is_strictly_subtype_of($other), is_strictly_supertype_of($other), is_strictly_a_type_of($other)

Stricter versions of the type comparison functions. These only care about explicit inheritance via parent.

   my $subtype_of_Num = Types::Standard::Num->create_child_type;
   my $subtype_of_Int = Types::Standard::Int->create_child_type;
   $subtype_of_Int->is_strictly_subtype_of( $subtype_of_Num );  # false
check($value)

Returns true iff the value passes the type constraint.

validate($value)

Returns the error message for the value; returns an explicit undef if the value passes the type constraint.

validate_explain($value, $varname)

Like validate but instead of a string error message, returns an arrayref of strings explaining the reasoning why the value does not meet the type constraint, examining parent types, etc.

The $varname is an optional string like '$foo' indicating the name of the variable being checked.

assert_valid($value)

Like check($value) but dies if the value does not pass the type constraint.

Yes, that's three very similar methods. Blame Moose::Meta::TypeConstraint whose API I'm attempting to emulate. :-)

assert_return($value)

Like assert_valid($value) but returns the value if it passes the type constraint.

This seems a more useful behaviour than assert_valid($value). I would have just changed assert_valid($value) to do this, except that there are edge cases where it could break Moose compatibility.

get_message($value)

Returns the error message for the value; even if the value passes the type constraint.

coerce($value)

Attempt to coerce $value to this type.

assert_coerce($value)

Attempt to coerce $value to this type. Throws an exception if this is not possible.

coercibles

Return a type constraint which is the union of type constraints that can be coerced to this one (including this one). If this type constraint has no coercions, returns itself.

can_be_inlined

Returns boolean indicating if this type can be inlined.

inline_check($varname)

Creates a type constraint check for a particular variable as a string of Perl code. For example:

   print( Types::Standard::Num->inline_check('$foo') );

prints the following output:

   (!ref($foo) && Scalar::Util::looks_like_number($foo))

For Moose-compat, there is an alias _inline_check for this method.

inline_assert($varname)

Much like inline_check but outputs a statement of the form:

   die ... unless ...;

Note that if this type has a custom error message, the inlined code will ignore this custom message!!

parameterize(@parameters)

Creates a new parameterized type; throws an exception if called on a non-parameterizable type.

create_child_type(%attributes)

Construct a new Type::Tiny object with this object as its parent.

child_type_class

The class that create_child_type will construct.

plus_coercions($type1, $code1, ...)

Shorthand for creating a new child type constraint with the same coercions as this one, but then adding some extra coercions (at a higher priority than the existing ones).

plus_fallback_coercions($type1, $code1, ...)

Like plus_coercions, but added at a lower priority.

minus_coercions($type1, ...)

Shorthand for creating a new child type constraint with fewer type coercions.

no_coercions

Shorthand for creating a new child type constraint with no coercions at all.

isa($class), can($method), AUTOLOAD(@args)

If Moose is loaded, then the combination of these methods is used to mock a Moose::Meta::TypeConstraint.

If Mouse is loaded, then isa mocks Mouse::Meta::TypeConstraint.

DOES($role)

Overridden to advertise support for various roles.

See also Type::API::Constraint, etc.

TIESCALAR, TIEARRAY, TIEHASH

These are provided as hooks that wrap Type::Tie. (Type::Tie is distributed separately, and can be used with non-Type::Tiny type constraints too.) They allow the following to work:

   use Types::Standard qw(Int);
   tie my @list, Int;
   push @list, 123, 456;   # ok
   push @list, "Hello";    # dies

The following methods exist for Moose/Mouse compatibility, but do not do anything useful.

compile_type_constraint
hand_optimized_type_constraint
has_hand_optimized_type_constraint
inline_environment
meta
type_parameter

Overloading

Constants

Type::Tiny::SUPPORT_SMARTMATCH

Indicates whether the smart match overload is supported on your version of Perl.

Package Variables

$Type::Tiny::DD

This in undef by default but may be set to a coderef that Type::Tiny and related modules will use to dump data structures in things like error messages.

Otherwise Type::Tiny uses it's own routine to dump data structures. $DD may then be set to a number to limit the lengths of the dumps. (Default limit is 72.)

This is a package variable (rather than get/set class methods) to allow for easy localization.

BUGS ^

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

SEE ALSO ^

Type::Tiny::Manual, Type::API.

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

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

Moose::Meta::TypeConstraint, Mouse::Meta::TypeConstraint.

Type::Params.

AUTHOR ^

Toby Inkster <tobyink@cpan.org>.

THANKS ^

Thanks to Matt S Trout for advice on Moo integration.

COPYRIGHT AND LICENCE ^

This software is copyright (c) 2013-2014 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.

syntax highlighting: