.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Type::Tiny 3" .TH Type::Tiny 3 2024-09-01 "perl v5.40.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Type::Tiny \- tiny, yet Moo(se)\-compatible type constraint .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 3 \& use v5.12; \& use strict; \& use warnings; \& \& package Horse { \& use Moo; \& use Types::Standard qw( Str Int Enum ArrayRef Object ); \& use Type::Params qw( signature ); \& use namespace::autoclean; \& \& has name => ( \& is => \*(Aqro\*(Aq, \& isa => Str, \& required => 1, \& ); \& has gender => ( \& is => \*(Aqro\*(Aq, \& isa => Enum[qw( f m )], \& ); \& has age => ( \& is => \*(Aqrw\*(Aq, \& isa => Int\->where( \*(Aq$_ >= 0\*(Aq ), \& ); \& has children => ( \& is => \*(Aqro\*(Aq, \& isa => ArrayRef[Object], \& default => sub { return [] }, \& ); \& \& sub add_child { \& state $check = signature( \& method => Object, \& positional => [ Object ], \& ); # method signature \& my ( $self, $child ) = $check\->( @_ ); # unpack @_ \& \& push @{ $self\->children }, $child; \& return $self; \& } \& } \& \& package main; \& \& my $boldruler = Horse\->new( \& name => "Bold Ruler", \& gender => \*(Aqm\*(Aq, \& age => 16, \& ); \& \& my $secretariat = Horse\->new( \& name => "Secretariat", \& gender => \*(Aqm\*(Aq, \& age => 0, \& ); \& \& $boldruler\->add_child( $secretariat ); .Ve .SH STATUS .IX Header "STATUS" This module is covered by the Type-Tiny stability policy. .SH DESCRIPTION .IX Header "DESCRIPTION" This documents the internals of the Type::Tiny class. Type::Tiny::Manual is a better starting place if you're new. .PP Type::Tiny is a small class for creating Moose-like type constraint objects which are compatible with Moo, Moose and Mouse. .PP .Vb 2 \& 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\*(Aqt 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); \& } .Ve .PP Type::Tiny conforms to Type::API::Constraint, Type::API::Constraint::Coercible, Type::API::Constraint::Constructor, and Type::API::Constraint::Inlinable. .PP Maybe now we won't need to have separate MooseX, MouseX and MooX versions of everything? We can but hope... .SS Constructor .IX Subsection "Constructor" .ie n .IP new(%attributes) 4 .el .IP \f(CWnew(%attributes)\fR 4 .IX Item "new(%attributes)" Moose-style constructor function. .SS Attributes .IX Subsection "Attributes" Attributes are named values that may be passed to the constructor. For each attribute, there is a corresponding reader method. For example: .PP .Vb 2 \& my $type = Type::Tiny\->new( name => "Foo" ); \& print $type\->name, "\en"; # says "Foo" .Ve .PP \fIImportant attributes\fR .IX Subsection "Important attributes" .PP These are the attributes you are likely to be most interested in providing when creating your own type constraints, and most interested in reading when dealing with type constraint objects. .ie n .IP """constraint""" 4 .el .IP \f(CWconstraint\fR 4 .IX Item "constraint" Coderef to validate a value (\f(CW$_\fR) against the type constraint. The coderef will not be called unless the value is known to pass any parent type constraint (see \f(CW\*(C`parent\*(C'\fR below). .Sp Alternatively, a string of Perl code checking \f(CW$_\fR can be passed as a parameter to the constructor, and will be converted to a coderef. .Sp Defaults to \f(CW\*(C`sub { 1 }\*(C'\fR \- i.e. a coderef that passes all values. .ie n .IP """parent""" 4 .el .IP \f(CWparent\fR 4 .IX Item "parent" Optional attribute; parent type constraint. For example, an "Integer" type constraint might have a parent "Number". .Sp If provided, must be a Type::Tiny object. .ie n .IP """inlined""" 4 .el .IP \f(CWinlined\fR 4 .IX Item "inlined" A coderef which returns a string of Perl code suitable for inlining this type. Optional. .Sp (The coderef will be called in list context and can actually return a list of strings which will be joined with \f(CW\*(C`&&\*(C'\fR. If the first item on the list is undef, it will be substituted with the type's parent's inline check.) .Sp If \f(CW\*(C`constraint\*(C'\fR (above) is a coderef generated via Sub::Quote, then Type::Tiny \fImay\fR be able to automatically generate \f(CW\*(C`inlined\*(C'\fR for you. If \f(CW\*(C`constraint\*(C'\fR (above) is a string, it will be able to. .ie n .IP """name""" 4 .el .IP \f(CWname\fR 4 .IX Item "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). .Sp Optional; if not supplied will be an anonymous type constraint. .ie n .IP """display_name""" 4 .el .IP \f(CWdisplay_name\fR 4 .IX Item "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 \f(CW\*(C`name\*(C'\fR. .ie n .IP """library""" 4 .el .IP \f(CWlibrary\fR 4 .IX Item "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. .ie n .IP """deprecated""" 4 .el .IP \f(CWdeprecated\fR 4 .IX Item "deprecated" Optional boolean indicating whether a type constraint is deprecated. Type::Library will issue a warning if you attempt to import a deprecated type constraint, but otherwise the type will continue to function as normal. There will not be deprecation warnings every time you validate a value, for instance. If omitted, defaults to the parent's deprecation status (or false if there's no parent). .ie n .IP """message""" 4 .el .IP \f(CWmessage\fR 4 .IX Item "message" Coderef that returns an error message when \f(CW$_\fR does not validate against the type constraint. Optional (there's a vaguely sensible default.) .ie n .IP """coercion""" 4 .el .IP \f(CWcoercion\fR 4 .IX Item "coercion" A Type::Coercion object associated with this type. .Sp Generally speaking this attribute should not be passed to the constructor; you should rely on the default lazily-built coercion object. .Sp You may pass \f(CW\*(C`coercion => 1\*(C'\fR to the constructor to inherit coercions from the constraint's parent. (This requires the parent constraint to have a coercion.) .ie n .IP """sorter""" 4 .el .IP \f(CWsorter\fR 4 .IX Item "sorter" A coderef which can be passed two values conforming to this type constraint and returns \-1, 0, or 1 to put them in order. Alternatively an arrayref containing a pair of coderefs — a sorter and a pre-processor for the Schwarzian transform. Optional. .Sp The idea is to allow for: .Sp .Vb 2 \& @sorted = Int\->sort( 2, 1, 11 ); # => 1, 2, 11 \& @sorted = Str\->sort( 2, 1, 11 ); # => 1, 11, 2 .Ve .ie n .IP """type_default""" 4 .el .IP \f(CWtype_default\fR 4 .IX Item "type_default" A coderef which returns a sensible default value for this type. For example, for a \fBCounter\fR type, a sensible default might be "0": .Sp .Vb 5 \& my $Size = Type::Tiny\->new( \& name => \*(AqSize\*(Aq, \& parent => Types::Standard::Enum[ qw( XS S M L XL ) ], \& type_default => sub { return \*(AqM\*(Aq; }, \& ); \& \& package Tshirt { \& use Moo; \& has size => ( \& is => \*(Aqro\*(Aq, \& isa => $Size, \& default => $Size\->type_default, \& ); \& } .Ve .Sp Child types will inherit a type default from their parent unless the child has a \f(CW\*(C`constraint\*(C'\fR. If a type neither has nor inherits a type default, then calling \f(CW\*(C`type_default\*(C'\fR will return undef. .Sp As a special case, this: .Sp .Vb 1 \& $type\->type_default( @args ) .Ve .Sp Will return: .Sp .Vb 4 \& sub { \& local $_ = \e@args; \& $type\->type_default\->( @_ ); \& } .Ve .Sp Many of the types defined in Types::Standard and other bundled type libraries have type defaults, but discovering them is left as an exercise for the reader. .ie n .IP """my_methods""" 4 .el .IP \f(CWmy_methods\fR 4 .IX Item "my_methods" Experimental hashref of additional methods that can be called on the type constraint object. .ie n .IP """exception_class""" 4 .el .IP \f(CWexception_class\fR 4 .IX Item "exception_class" The class used to throw an exception when a value fails its type check. Defaults to "Error::TypeTiny::Assertion", which is usually good. This class is expected to provide a \f(CW\*(C`throw_cb\*(C'\fR method compatible with the method of that name in Error::TypeTiny. .Sp If a parent type constraint has a custom \f(CW\*(C`exception_class\*(C'\fR, then this will be "inherited" by its children. .PP \fIAttributes related to parameterizable and parameterized types\fR .IX Subsection "Attributes related to parameterizable and parameterized types" .PP The following additional attributes are used for parameterizable (e.g. \&\f(CW\*(C`ArrayRef\*(C'\fR) and parameterized (e.g. \f(CW\*(C`ArrayRef[Int]\*(C'\fR) type constraints. Unlike Moose, these aren't handled by separate subclasses. .ie n .IP """constraint_generator""" 4 .el .IP \f(CWconstraint_generator\fR 4 .IX Item "constraint_generator" Coderef that is called when a type constraint is parameterized. When called, it is passed the list of parameters, though any parameter which looks like a foreign type constraint (Moose type constraints, Mouse type constraints, etc, \&\fIand coderefs(!!!)\fR) is first coerced to a native Type::Tiny object. .Sp Note that for compatibility with the Moose API, the base type is \fInot\fR passed to the constraint generator, but can be found in the package variable \&\f(CW$Type::Tiny::parameterize_type\fR. The first parameter is also available as \f(CW$_\fR. .Sp Types \fIcan\fR be parameterized with an empty parameter list. For example, in Types::Standard, \f(CW\*(C`Tuple\*(C'\fR is just an alias for \f(CW\*(C`ArrayRef\*(C'\fR but \&\f(CW\*(C`Tuple[]\*(C'\fR will only allow zero-length arrayrefs to pass the constraint. If you wish \f(CW\*(C`YourType\*(C'\fR and \f(CW\*(C`YourType[]\*(C'\fR to mean the same thing, then do: .Sp .Vb 1 \& return $Type::Tiny::parameterize_type unless @_; .Ve .Sp The constraint generator should generate and return a new constraint coderef based on the parameters. Alternatively, the constraint generator can return a fully-formed Type::Tiny object, in which case the \f(CW\*(C`name_generator\*(C'\fR, \&\f(CW\*(C`inline_generator\*(C'\fR, and \f(CW\*(C`coercion_generator\*(C'\fR attributes documented below are ignored. .Sp Optional; providing a generator makes this type into a parameterizable type constraint. If there is no generator, attempting to parameterize the type constraint will throw an exception. .ie n .IP """name_generator""" 4 .el .IP \f(CWname_generator\fR 4 .IX Item "name_generator" A coderef which generates a new display_name based on parameters. Called with the same parameters and package variables as the \f(CW\*(C`constraint_generator\*(C'\fR. Expected to return a string. .Sp Optional; the default is reasonable. .ie n .IP """inline_generator""" 4 .el .IP \f(CWinline_generator\fR 4 .IX Item "inline_generator" A coderef which generates a new inlining coderef based on parameters. Called with the same parameters and package variables as the \f(CW\*(C`constraint_generator\*(C'\fR. Expected to return a coderef. .Sp Optional. .ie n .IP """coercion_generator""" 4 .el .IP \f(CWcoercion_generator\fR 4 .IX Item "coercion_generator" A coderef which generates a new Type::Coercion object based on parameters. Called with the same parameters and package variables as the \&\f(CW\*(C`constraint_generator\*(C'\fR. Expected to return a blessed object. .Sp Optional. .ie n .IP """deep_explanation""" 4 .el .IP \f(CWdeep_explanation\fR 4 .IX Item "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. .ie n .IP """parameters""" 4 .el .IP \f(CWparameters\fR 4 .IX Item "parameters" In parameterized types, returns an arrayref of the parameters. .PP \fILazy generated attributes\fR .IX Subsection "Lazy generated attributes" .PP The following attributes should not be usually passed to the constructor; unless you're doing something especially unusual, you should rely on the default lazily-built return values. .ie n .IP """compiled_check""" 4 .el .IP \f(CWcompiled_check\fR 4 .IX Item "compiled_check" Coderef to validate a value (\f(CW$_[0]\fR) against the type constraint. This coderef is expected to also handle all validation for the parent type constraints. .ie n .IP """definition_context""" 4 .el .IP \f(CWdefinition_context\fR 4 .IX Item "definition_context" Hashref of information indicating where the type constraint was originally defined. Type::Tiny will generate this based on \f(CW\*(C`caller\*(C'\fR if you do not supply it. The hashref will ordinarily contain keys \f(CW"package"\fR, \f(CW"file"\fR, and \f(CW"line"\fR. .Sp For parameterized types and compound types (e.g. unions and intersections), this may not be especially meaningful information. .ie n .IP """complementary_type""" 4 .el .IP \f(CWcomplementary_type\fR 4 .IX Item "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. .ie n .IP """moose_type"", ""mouse_type""" 4 .el .IP "\f(CWmoose_type\fR, \f(CWmouse_type\fR" 4 .IX Item "moose_type, mouse_type" Objects equivalent to this type constraint, but as a Moose::Meta::TypeConstraint or Mouse::Meta::TypeConstraint. .Sp 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. .SS Methods .IX Subsection "Methods" \fIPredicate methods\fR .IX Subsection "Predicate methods" .PP These methods return booleans indicating information about the type constraint. They are each tightly associated with a particular attribute. (See "Attributes".) .ie n .IP """has_parent"", ""has_library"", ""has_inlined"", ""has_constraint_generator"", ""has_inline_generator"", ""has_coercion_generator"", ""has_parameters"", ""has_message"", ""has_deep_explanation"", ""has_sorter""" 4 .el .IP "\f(CWhas_parent\fR, \f(CWhas_library\fR, \f(CWhas_inlined\fR, \f(CWhas_constraint_generator\fR, \f(CWhas_inline_generator\fR, \f(CWhas_coercion_generator\fR, \f(CWhas_parameters\fR, \f(CWhas_message\fR, \f(CWhas_deep_explanation\fR, \f(CWhas_sorter\fR" 4 .IX Item "has_parent, has_library, has_inlined, has_constraint_generator, has_inline_generator, has_coercion_generator, has_parameters, has_message, has_deep_explanation, has_sorter" Simple Moose-style predicate methods indicating the presence or absence of an attribute. .ie n .IP """has_coercion""" 4 .el .IP \f(CWhas_coercion\fR 4 .IX Item "has_coercion" Predicate method with a little extra DWIM. Returns false if the coercion is a no-op. .ie n .IP """is_anon""" 4 .el .IP \f(CWis_anon\fR 4 .IX Item "is_anon" Returns true iff the type constraint does not have a \f(CW\*(C`name\*(C'\fR. .ie n .IP """is_parameterized"", ""is_parameterizable""" 4 .el .IP "\f(CWis_parameterized\fR, \f(CWis_parameterizable\fR" 4 .IX Item "is_parameterized, is_parameterizable" Indicates whether a type has been parameterized (e.g. \f(CW\*(C`ArrayRef[Int]\*(C'\fR) or could potentially be (e.g. \f(CW\*(C`ArrayRef\*(C'\fR). .ie n .IP """has_parameterized_from""" 4 .el .IP \f(CWhas_parameterized_from\fR 4 .IX Item "has_parameterized_from" Useless alias for \f(CW\*(C`is_parameterized\*(C'\fR. .PP \fIValidation and coercion\fR .IX Subsection "Validation and coercion" .PP The following methods are used for coercing and validating values against a type constraint: .ie n .IP check($value) 4 .el .IP \f(CWcheck($value)\fR 4 .IX Item "check($value)" Returns true iff the value passes the type constraint. .ie n .IP validate($value) 4 .el .IP \f(CWvalidate($value)\fR 4 .IX Item "validate($value)" Returns the error message for the value; returns an explicit undef if the value passes the type constraint. .ie n .IP assert_valid($value) 4 .el .IP \f(CWassert_valid($value)\fR 4 .IX Item "assert_valid($value)" Like \f(CWcheck($value)\fR but dies if the value does not pass the type constraint. .Sp Yes, that's three very similar methods. Blame Moose::Meta::TypeConstraint whose API I'm attempting to emulate. :\-) .ie n .IP assert_return($value) 4 .el .IP \f(CWassert_return($value)\fR 4 .IX Item "assert_return($value)" Like \f(CWassert_valid($value)\fR but returns the value if it passes the type constraint. .Sp This seems a more useful behaviour than \f(CWassert_valid($value)\fR. I would have just changed \f(CWassert_valid($value)\fR to do this, except that there are edge cases where it could break Moose compatibility. .ie n .IP get_message($value) 4 .el .IP \f(CWget_message($value)\fR 4 .IX Item "get_message($value)" Returns the error message for the value; even if the value passes the type constraint. .ie n .IP """validate_explain($value, $varname)""" 4 .el .IP "\f(CWvalidate_explain($value, $varname)\fR" 4 .IX Item "validate_explain($value, $varname)" Like \f(CW\*(C`validate\*(C'\fR 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. .Sp The \f(CW$varname\fR is an optional string like \f(CW\*(Aq$foo\*(Aq\fR indicating the name of the variable being checked. .ie n .IP coerce($value) 4 .el .IP \f(CWcoerce($value)\fR 4 .IX Item "coerce($value)" Attempt to coerce \f(CW$value\fR to this type. .ie n .IP assert_coerce($value) 4 .el .IP \f(CWassert_coerce($value)\fR 4 .IX Item "assert_coerce($value)" Attempt to coerce \f(CW$value\fR to this type. Throws an exception if this is not possible. .PP \fIChild type constraint creation and parameterization\fR .IX Subsection "Child type constraint creation and parameterization" .PP These methods generate new type constraint objects that inherit from the constraint they are called upon: .ie n .IP create_child_type(%attributes) 4 .el .IP \f(CWcreate_child_type(%attributes)\fR 4 .IX Item "create_child_type(%attributes)" Construct a new Type::Tiny object with this object as its parent. .ie n .IP where($coderef) 4 .el .IP \f(CWwhere($coderef)\fR 4 .IX Item "where($coderef)" Shortcut for creating an anonymous child type constraint. Use it like \&\f(CW\*(C`HashRef\->where(sub { exists($_\->{name}) })\*(C'\fR. That said, you can get a similar result using overloaded \f(CW\*(C`&\*(C'\fR: .Sp .Vb 1 \& HashRef & sub { exists($_\->{name}) } .Ve .Sp Like the \f(CW\*(C`constraint\*(C'\fR attribute, this will accept a string of Perl code: .Sp .Vb 1 \& HashRef\->where(\*(Aqexists($_\->{name})\*(Aq) .Ve .ie n .IP """child_type_class""" 4 .el .IP \f(CWchild_type_class\fR 4 .IX Item "child_type_class" The class that create_child_type will construct by default. .ie n .IP parameterize(@parameters) 4 .el .IP \f(CWparameterize(@parameters)\fR 4 .IX Item "parameterize(@parameters)" Creates a new parameterized type; throws an exception if called on a non-parameterizable type. .ie n .IP of(@parameters) 4 .el .IP \f(CWof(@parameters)\fR 4 .IX Item "of(@parameters)" A cute alias for \f(CW\*(C`parameterize\*(C'\fR. Use it like \f(CW\*(C`ArrayRef\->of(Int)\*(C'\fR. .ie n .IP """plus_coercions($type1, $code1, ...)""" 4 .el .IP "\f(CWplus_coercions($type1, $code1, ...)\fR" 4 .IX Item "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). .ie n .IP """plus_fallback_coercions($type1, $code1, ...)""" 4 .el .IP "\f(CWplus_fallback_coercions($type1, $code1, ...)\fR" 4 .IX Item "plus_fallback_coercions($type1, $code1, ...)" Like \f(CW\*(C`plus_coercions\*(C'\fR, but added at a lower priority. .ie n .IP """minus_coercions($type1, ...)""" 4 .el .IP "\f(CWminus_coercions($type1, ...)\fR" 4 .IX Item "minus_coercions($type1, ...)" Shorthand for creating a new child type constraint with fewer type coercions. .ie n .IP """no_coercions""" 4 .el .IP \f(CWno_coercions\fR 4 .IX Item "no_coercions" Shorthand for creating a new child type constraint with no coercions at all. .PP \fIType relationship introspection methods\fR .IX Subsection "Type relationship introspection methods" .PP These methods allow you to determine a type constraint's relationship to other type constraints in an organised hierarchy: .ie n .IP "equals($other), is_subtype_of($other), is_supertype_of($other), is_a_type_of($other)" 4 .el .IP "\f(CWequals($other)\fR, \f(CWis_subtype_of($other)\fR, \f(CWis_supertype_of($other)\fR, \f(CWis_a_type_of($other)\fR" 4 .IX Item "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 \f(CW\*(C`is_supertype_of\*(C'\fR, but you get the idea, right?) .Sp 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: .Sp .Vb 3 \& 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 .Ve .ie n .IP "strictly_equals($other), is_strictly_subtype_of($other), is_strictly_supertype_of($other), is_strictly_a_type_of($other)" 4 .el .IP "\f(CWstrictly_equals($other)\fR, \f(CWis_strictly_subtype_of($other)\fR, \f(CWis_strictly_supertype_of($other)\fR, \f(CWis_strictly_a_type_of($other)\fR" 4 .IX Item "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 \f(CW\*(C`parent\*(C'\fR. .Sp .Vb 3 \& 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 .Ve .ie n .IP """parents""" 4 .el .IP \f(CWparents\fR 4 .IX Item "parents" Returns a list of all this type constraint's ancestor constraints. For example, if called on the \f(CW\*(C`Str\*(C'\fR type constraint would return the list \&\f(CW\*(C`(Value, Defined, Item, Any)\*(C'\fR. .Sp \&\fIDue to a historical misunderstanding, this differs from the Moose implementation of the \fR\f(CI\*(C`parents\*(C'\fR\fI method. In Moose, \fR\f(CI\*(C`parents\*(C'\fR\fI only returns the immediate parent type constraints, and because type constraints only have one immediate parent, this is effectively an alias for \fR\f(CI\*(C`parent\*(C'\fR\fI. 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.\fR .ie n .IP find_parent($coderef) 4 .el .IP \f(CWfind_parent($coderef)\fR 4 .IX Item "find_parent($coderef)" Loops through the parent type constraints \fIincluding the invocant itself\fR and returns the nearest ancestor type constraint where the coderef evaluates to true. Within the coderef the ancestor currently being checked is \f(CW$_\fR. Returns undef if there is no match. .Sp In list context also returns the number of type constraints which had been looped through before the matching constraint was found. .ie n .IP """find_constraining_type""" 4 .el .IP \f(CWfind_constraining_type\fR 4 .IX Item "find_constraining_type" Finds the nearest ancestor type constraint (including the type itself) which has a \f(CW\*(C`constraint\*(C'\fR coderef. .Sp Equivalent to: .Sp .Vb 1 \& $type\->find_parent(sub { not $_\->_is_null_constraint }) .Ve .ie n .IP """coercibles""" 4 .el .IP \f(CWcoercibles\fR 4 .IX Item "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. .ie n .IP """type_parameter""" 4 .el .IP \f(CWtype_parameter\fR 4 .IX Item "type_parameter" In parameterized type constraints, returns the first item on the list of parameters; otherwise returns undef. For example: .Sp .Vb 2 \& ( ArrayRef[Int] )\->type_parameter; # returns Int \& ( ArrayRef[Int] )\->parent; # returns ArrayRef .Ve .Sp Note that parameterizable type constraints can perfectly legitimately take multiple parameters (several of the parameterizable type constraints in Types::Standard do). This method only returns the first such parameter. "Attributes related to parameterizable and parameterized types" documents the \f(CW\*(C`parameters\*(C'\fR attribute, which returns an arrayref of all the parameters. .ie n .IP """parameterized_from""" 4 .el .IP \f(CWparameterized_from\fR 4 .IX Item "parameterized_from" Harder to spell alias for \f(CW\*(C`parent\*(C'\fR that only works for parameterized types. .PP \&\fIHint for people subclassing Type::Tiny:\fR Since version 1.006000, the methods for determining subtype, supertype, and type equality should \fInot\fR be overridden in subclasses of Type::Tiny. This is because of the problem of diamond inheritance. If X and Y are both subclasses of Type::Tiny, they \fIboth\fR need to be consulted to figure out how type constraints are related; not just one of them should be overriding these methods. See the source code for Type::Tiny::Enum for an example of how subclasses can give hints about type relationships to Type::Tiny. Summary: push a coderef onto \f(CW@Type::Tiny::CMP\fR. This coderef will be passed two type constraints. It should then return one of the constants Type::Tiny::CMP_SUBTYPE (first type is a subtype of second type), Type::Tiny::CMP_SUPERTYPE (second type is a subtype of first type), Type::Tiny::CMP_EQUAL (the two types are exactly the same), Type::Tiny::CMP_EQUIVALENT (the two types are effectively the same), or Type::Tiny::CMP_UNKNOWN (your coderef couldn't establish any relationship). .PP \fIType relationship introspection function\fR .IX Subsection "Type relationship introspection function" .ie n .IP """Type::Tiny::cmp($type1, $type2)""" 4 .el .IP "\f(CWType::Tiny::cmp($type1, $type2)\fR" 4 .IX Item "Type::Tiny::cmp($type1, $type2)" The subtype/supertype relationship between types results in a partial ordering of type constraints. .Sp This function will return one of the constants: Type::Tiny::CMP_SUBTYPE (first type is a subtype of second type), Type::Tiny::CMP_SUPERTYPE (second type is a subtype of first type), Type::Tiny::CMP_EQUAL (the two types are exactly the same), Type::Tiny::CMP_EQUIVALENT (the two types are effectively the same), or Type::Tiny::CMP_UNKNOWN (couldn't establish any relationship). In numeric contexts, these evaluate to \-1, 1, 0, 0, and 0, making it potentially usable with \f(CW\*(C`sort\*(C'\fR (though you may need to silence warnings about treating the empty string as a numeric value). .PP \fIList processing methods\fR .IX Subsection "List processing methods" .ie n .IP grep(@list) 4 .el .IP \f(CWgrep(@list)\fR 4 .IX Item "grep(@list)" Filters a list to return just the items that pass the type check. .Sp .Vb 1 \& @integers = Int\->grep(@list); .Ve .ie n .IP first(@list) 4 .el .IP \f(CWfirst(@list)\fR 4 .IX Item "first(@list)" Filters the list to return the first item on the list that passes the type check, or undef if none do. .Sp .Vb 1 \& $first_lady = Woman\->first(@people); .Ve .ie n .IP map(@list) 4 .el .IP \f(CWmap(@list)\fR 4 .IX Item "map(@list)" Coerces a list of items. Only works on types which have a coercion. .Sp .Vb 1 \& @truths = Bool\->map(@list); .Ve .ie n .IP sort(@list) 4 .el .IP \f(CWsort(@list)\fR 4 .IX Item "sort(@list)" Sorts a list of items according to the type's preferred sorting mechanism, or if the type doesn't have a sorter coderef, uses the parent type. If no ancestor type constraint has a sorter, throws an exception. The \f(CW\*(C`Str\*(C'\fR, \&\f(CW\*(C`StrictNum\*(C'\fR, \f(CW\*(C`LaxNum\*(C'\fR, and \f(CW\*(C`Enum\*(C'\fR type constraints include sorters. .Sp .Vb 1 \& @sorted_numbers = Num\->sort( Num\->grep(@list) ); .Ve .ie n .IP rsort(@list) 4 .el .IP \f(CWrsort(@list)\fR 4 .IX Item "rsort(@list)" Like \f(CW\*(C`sort\*(C'\fR but backwards. .ie n .IP any(@list) 4 .el .IP \f(CWany(@list)\fR 4 .IX Item "any(@list)" Returns true if any of the list match the type. .Sp .Vb 3 \& if ( Int\->any(@numbers) ) { \& say "there was at least one integer"; \& } .Ve .ie n .IP all(@list) 4 .el .IP \f(CWall(@list)\fR 4 .IX Item "all(@list)" Returns true if all of the list match the type. .Sp .Vb 3 \& if ( Int\->all(@numbers) ) { \& say "they were all integers"; \& } .Ve .ie n .IP assert_any(@list) 4 .el .IP \f(CWassert_any(@list)\fR 4 .IX Item "assert_any(@list)" Like \f(CW\*(C`any\*(C'\fR but instead of returning a boolean, returns the entire original list if any item on it matches the type, and dies if none does. .ie n .IP assert_all(@list) 4 .el .IP \f(CWassert_all(@list)\fR 4 .IX Item "assert_all(@list)" Like \f(CW\*(C`all\*(C'\fR but instead of returning a boolean, returns the original list if all items on it match the type, but dies as soon as it finds one that does not. .PP \fIInlining methods\fR .IX Subsection "Inlining methods" .PP The following methods are used to generate strings of Perl code which may be pasted into stringy \f(CW\*(C`eval\*(C'\fRuated subs to perform type checks: .ie n .IP """can_be_inlined""" 4 .el .IP \f(CWcan_be_inlined\fR 4 .IX Item "can_be_inlined" Returns boolean indicating if this type can be inlined. .ie n .IP inline_check($varname) 4 .el .IP \f(CWinline_check($varname)\fR 4 .IX Item "inline_check($varname)" Creates a type constraint check for a particular variable as a string of Perl code. For example: .Sp .Vb 1 \& print( Types::Standard::Num\->inline_check(\*(Aq$foo\*(Aq) ); .Ve .Sp prints the following output: .Sp .Vb 1 \& (!ref($foo) && Scalar::Util::looks_like_number($foo)) .Ve .Sp For Moose-compat, there is an alias \f(CW\*(C`_inline_check\*(C'\fR for this method. .ie n .IP inline_assert($varname) 4 .el .IP \f(CWinline_assert($varname)\fR 4 .IX Item "inline_assert($varname)" Much like \f(CW\*(C`inline_check\*(C'\fR but outputs a statement of the form: .Sp .Vb 1 \& ... or die ...; .Ve .Sp Can also be called line \f(CW\*(C`inline_assert($varname, $typevarname, %extras)\*(C'\fR. In this case, it will generate a string of code that may include \&\f(CW$typevarname\fR which is supposed to be the name of a variable holding the type itself. (This is kinda complicated, but it allows a useful string to still be produced if the type is not inlineable.) The \f(CW%extras\fR are additional options to be passed to Error::TypeTiny::Assertion's constructor and must be key-value pairs of strings only, no references or undefs. .PP \fIOther methods\fR .IX Subsection "Other methods" .ie n .IP """qualified_name""" 4 .el .IP \f(CWqualified_name\fR 4 .IX Item "qualified_name" For non-anonymous type constraints that have a library, returns a qualified \&\f(CW"MyLib::MyType"\fR sort of name. Otherwise, returns the same as \f(CW\*(C`name\*(C'\fR. .ie n .IP "isa($class), can($method), AUTOLOAD(@args)" 4 .el .IP "\f(CWisa($class)\fR, \f(CWcan($method)\fR, \f(CWAUTOLOAD(@args)\fR" 4 .IX Item "isa($class), can($method), AUTOLOAD(@args)" If Moose is loaded, then the combination of these methods is used to mock a Moose::Meta::TypeConstraint. .Sp If Mouse is loaded, then \f(CW\*(C`isa\*(C'\fR mocks Mouse::Meta::TypeConstraint. .ie n .IP DOES($role) 4 .el .IP \f(CWDOES($role)\fR 4 .IX Item "DOES($role)" Overridden to advertise support for various roles. .Sp See also Type::API::Constraint, etc. .ie n .IP """TIESCALAR"", ""TIEARRAY"", ""TIEHASH""" 4 .el .IP "\f(CWTIESCALAR\fR, \f(CWTIEARRAY\fR, \f(CWTIEHASH\fR" 4 .IX Item "TIESCALAR, TIEARRAY, TIEHASH" These are provided as hooks that wrap Type::Tie. They allow the following to work: .Sp .Vb 4 \& use Types::Standard qw(Int); \& tie my @list, Int; \& push @list, 123, 456; # ok \& push @list, "Hello"; # dies .Ve .ie n .IP "exportables( $base_name )" 4 .el .IP "\f(CWexportables( $base_name )\fR" 4 .IX Item "exportables( $base_name )" Returns a list of the functions a type library should export if it contains this type constraint. .Sp Example: .Sp .Vb 6 \& [ \& { name => \*(AqInt\*(Aq, tags => [ \*(Aqtypes\*(Aq ], code => sub { ... } }, \& { name => \*(Aqis_Int\*(Aq, tags => [ \*(Aqis\*(Aq ], code => sub { ... } }, \& { name => \*(Aqassert_Int\*(Aq, tags => [ \*(Aqassert\*(Aq ], code => sub { ... } }, \& { name => \*(Aqto_Int\*(Aq, tags => [ \*(Aqto\*(Aq ], code => sub { ... } }, \& ] .Ve .Sp \&\f(CW$base_name\fR is optional, but allows you to get a list of exportables using a specific name. This is useful if the type constraint has a name which wouldn't be a legal Perl function name. .ie n .IP """exportables_by_tag( $tag, $base_name )""" 4 .el .IP "\f(CWexportables_by_tag( $tag, $base_name )\fR" 4 .IX Item "exportables_by_tag( $tag, $base_name )" Filters \f(CW\*(C`exportables\*(C'\fR by a specific tag name. In list context, returns all matching exportables. In scalar context returns a single matching exportable and dies if multiple exportables match, or none do! .PP The following methods exist for Moose/Mouse compatibility, but do not do anything useful. .ie n .IP """compile_type_constraint""" 4 .el .IP \f(CWcompile_type_constraint\fR 4 .IX Item "compile_type_constraint" .PD 0 .ie n .IP """hand_optimized_type_constraint""" 4 .el .IP \f(CWhand_optimized_type_constraint\fR 4 .IX Item "hand_optimized_type_constraint" .ie n .IP """has_hand_optimized_type_constraint""" 4 .el .IP \f(CWhas_hand_optimized_type_constraint\fR 4 .IX Item "has_hand_optimized_type_constraint" .ie n .IP """inline_environment""" 4 .el .IP \f(CWinline_environment\fR 4 .IX Item "inline_environment" .ie n .IP """meta""" 4 .el .IP \f(CWmeta\fR 4 .IX Item "meta" .PD .SS Overloading .IX Subsection "Overloading" .IP \(bu 4 Stringification is overloaded to return the qualified name. .IP \(bu 4 Boolification is overloaded to always return true. .IP \(bu 4 Coderefification is overloaded to call \f(CW\*(C`assert_return\*(C'\fR. .IP \(bu 4 On Perl 5.10.1 and above, smart match is overloaded to call \f(CW\*(C`check\*(C'\fR. .IP \(bu 4 The \f(CW\*(C`==\*(C'\fR operator is overloaded to call \f(CW\*(C`equals\*(C'\fR. .IP \(bu 4 The \f(CW\*(C`<\*(C'\fR and \f(CW\*(C`>\*(C'\fR operators are overloaded to call \f(CW\*(C`is_subtype_of\*(C'\fR and \f(CW\*(C`is_supertype_of\*(C'\fR. .IP \(bu 4 The \f(CW\*(C`~\*(C'\fR operator is overloaded to call \f(CW\*(C`complementary_type\*(C'\fR. .IP \(bu 4 The \f(CW\*(C`|\*(C'\fR operator is overloaded to build a union of two type constraints. See Type::Tiny::Union. .IP \(bu 4 The \f(CW\*(C`&\*(C'\fR operator is overloaded to build the intersection of two type constraints. See Type::Tiny::Intersection. .IP \(bu 4 The \f(CW\*(C`/\*(C'\fR operator provides magical Devel::StrictMode support. If \f(CW$ENV{PERL_STRICT}\fR (or a few other environment variables) is true, then it returns the left operand. Normally it returns the right operand. .PP Previous versions of Type::Tiny would overload the \f(CW\*(C`+\*(C'\fR operator to call \f(CW\*(C`plus_coercions\*(C'\fR or \f(CW\*(C`plus_fallback_coercions\*(C'\fR as appropriate. Support for this was dropped after 0.040. .SS Constants .IX Subsection "Constants" .ie n .IP """Type::Tiny::SUPPORT_SMARTMATCH""" 4 .el .IP \f(CWType::Tiny::SUPPORT_SMARTMATCH\fR 4 .IX Item "Type::Tiny::SUPPORT_SMARTMATCH" Indicates whether the smart match overload is supported on your version of Perl. .SS "Package Variables" .IX Subsection "Package Variables" .ie n .IP $Type::Tiny::DD 4 .el .IP \f(CW$Type::Tiny::DD\fR 4 .IX Item "$Type::Tiny::DD" This 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. .Sp Otherwise Type::Tiny uses it's own routine to dump data structures. \&\f(CW$DD\fR may then be set to a number to limit the lengths of the dumps. (Default limit is 72.) .Sp This is a package variable (rather than get/set class methods) to allow for easy localization. .ie n .IP $Type::Tiny::AvoidCallbacks 4 .el .IP \f(CW$Type::Tiny::AvoidCallbacks\fR 4 .IX Item "$Type::Tiny::AvoidCallbacks" If this variable is set to true (you should usually do it in a \&\f(CW\*(C`local\*(C'\fR scope), it acts as a hint for type constraints, when generating inlined code, to avoid making any callbacks to variables and functions defined outside the inlined code itself. .Sp This should have the effect that \f(CW\*(C`$type\->inline_check(\*(Aq$foo\*(Aq)\*(C'\fR will return a string of code capable of checking the type on Perl installations that don't have Type::Tiny installed. This is intended to allow Type::Tiny to be used with things like Mite. .Sp The variable works on the honour system. Types need to explicitly check it and decide to generate different code based on its truth value. The bundled types in Types::Standard, Types::Common::Numeric, and Types::Common::String all do. (\fBStrMatch\fR is sometimes unable to, and will issue a warning if it needs to rely on callbacks when asked not to.) .Sp Most normal users can ignore this. .ie n .IP $Type::Tiny::SafePackage 4 .el .IP \f(CW$Type::Tiny::SafePackage\fR 4 .IX Item "$Type::Tiny::SafePackage" This is the string "package Type::Tiny;" which is sometimes inserted into strings of inlined code to avoid namespace clashes. In most cases, you do not need to change this. However, if you are inlining type constraint code, saving that code into Perl modules, and uploading them to CPAN, you may wish to change it to avoid problems with the CPAN indexer. Most normal users of Type::Tiny do not need to be aware of this. .SS Environment .IX Subsection "Environment" .ie n .IP """PERL_TYPE_TINY_XS""" 4 .el .IP \f(CWPERL_TYPE_TINY_XS\fR 4 .IX Item "PERL_TYPE_TINY_XS" Currently this has more effect on Types::Standard than Type::Tiny. In future it may be used to trigger or suppress the loading XS implementations of parts of Type::Tiny. .SH BUGS .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" The Type::Tiny homepage . .PP Type::Tiny::Manual, Type::API. .PP Type::Library, Type::Utils, Types::Standard, Type::Coercion. .PP Type::Tiny::Class, Type::Tiny::Role, Type::Tiny::Duck, Type::Tiny::Enum, Type::Tiny::Union, Type::Tiny::Intersection. .PP Moose::Meta::TypeConstraint, Mouse::Meta::TypeConstraint. .PP Type::Params. .PP Type::Tiny on GitHub , Type::Tiny on Travis-CI , Type::Tiny on AppVeyor , Type::Tiny on Codecov , Type::Tiny on Coveralls . .SH AUTHOR .IX Header "AUTHOR" Toby Inkster . .SH THANKS .IX Header "THANKS" Thanks to Matt S Trout for advice on Moo integration. .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2013\-2014, 2017\-2023 by Toby Inkster. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "DISCLAIMER OF WARRANTIES" .IX Header "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.