# phpman > perldoc > Type::Library

## NAME
    [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown) - tiny, yet Moo(se)-compatible type libraries

## SYNOPSIS
       package [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) {
          use [Scalar::Util](https://www.chedong.com/phpMan.php/perldoc/Scalar%3A%3AUtil/markdown) qw(looks_like_number);
          use [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown) -base;
          use [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown);

          my $NUM = "[Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown)"->new(
             name       => "Number",
             constraint => sub { looks_like_number($_) },
             message    => sub { "$_ ain't a number" },
          );

          __PACKAGE__->meta->add_type($NUM);

          __PACKAGE__->meta->make_immutable;
       }

       package Ermintrude {
          use Moo;
          use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw(Number);
          has favourite_number => (is => "ro", isa => Number);
       }

       package Bullwinkle {
          use Moose;
          use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw(Number);
          has favourite_number => (is => "ro", isa => Number);
       }

       package Maisy {
          use Mouse;
          use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw(Number);
          has favourite_number => (is => "ro", isa => Number);
       }

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

## DESCRIPTION
    [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown) is a tiny class for creating [MooseX::Types](https://www.chedong.com/phpMan.php/perldoc/MooseX%3A%3ATypes/markdown)-like type libraries which are
    compatible with Moo, Moose and Mouse.

    If you're reading this because you want to create a type library, then you're probably better
    off reading [Type::Tiny::Manual::Libraries](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3ALibraries/markdown).

### Methods
    A type library is a singleton class. Use the "meta" method to get a blessed object which other
    methods can get called on. For example:

       [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown)->meta->add_type($foo);

    "add_type($type)" or "add_type(%opts)"
        Add a type to the library. If %opts is given, then this method calls
        "[Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown)->new(%opts)" first, and adds the resultant type.

        Adding a type named "Foo" to the library will automatically define four functions in the
        library's namespace:

        "Foo"
            Returns the [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) object.

        "is_Foo($value)"
            Returns true iff $value passes the type constraint.

        "assert_Foo($value)"
            Returns $value iff $value passes the type constraint. Dies otherwise.

        "to_Foo($value)"
            Coerces the value to the type.

    "get_type($name)"
        Gets the "[Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown)" object corresponding to the name.

    "has_type($name)"
        Boolean; returns true if the type exists in the library.

    "type_names"
        List all types defined by the library.

    "add_coercion($c)" or "add_coercion(%opts)"
        Add a standalone coercion to the library. If %opts is given, then this method calls
        "[Type::Coercion](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ACoercion/markdown)->new(%opts)" first, and adds the resultant coercion.

        Adding a coercion named "FooFromBar" to the library will automatically define a function in
        the library's namespace:

        "FooFromBar"
            Returns the [Type::Coercion](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ACoercion/markdown) object.

    "get_coercion($name)"
        Gets the "[Type::Coercion](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ACoercion/markdown)" object corresponding to the name.

    "has_coercion($name)"
        Boolean; returns true if the coercion exists in the library.

    "coercion_names"
        List all standalone coercions defined by the library.

    "import(@args)"
        [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown)-based libraries are exporters.

    "make_immutable"
        A shortcut for calling "$type->coercion->freeze" on every type constraint in the library.

### Constants
    "NICE_PROTOTYPES"
        If this is true, then [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown) will give parameterizable type constraints slightly the
        nicer prototype of "(;$)" instead of the default "(;@)". This allows constructs like:

           ArrayRef[Int] | HashRef[Int]

        ... to "just work".

### Export
    Type libraries are exporters. For the purposes of the following examples, assume that the
    "[Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown)" library defines types "Number" and "String".

       # Exports nothing.
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown);

       # Exports a function "String" which is a constant returning
       # the String type constraint.
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( String );

       # Exports both String and Number as above.
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( String Number );

       # Same.
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( :types );

       # Exports "coerce_String" and "coerce_Number", as well as any other
       # coercions
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( :coercions );

       # Exports a sub "is_String" so that "is_String($foo)" is equivalent
       # to "String->check($foo)".
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( is_String );

       # Exports "is_String" and "is_Number".
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( :is );

       # Exports a sub "assert_String" so that "assert_String($foo)" is
       # equivalent to "String->assert_return($foo)".
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( assert_String );

       # Exports "assert_String" and "assert_Number".
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( :assert );

       # Exports a sub "to_String" so that "to_String($foo)" is equivalent
       # to "String->coerce($foo)".
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( to_String );

       # Exports "to_String" and "to_Number".
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( :to );

       # Exports "String", "is_String", "assert_String" and "coerce_String".
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( +String );

       # Exports everything.
       #
       use [Types::Mine](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AMine/markdown) qw( :all );

    Type libraries automatically inherit from [Exporter::Tiny](https://www.chedong.com/phpMan.php/perldoc/Exporter%3A%3ATiny/markdown); see the documentation of that module
    for tips and tricks importing from libraries.

## BUGS
    Please report any bugs to <<https://github.com/tobyink/p5-type-tiny/issues>>.

## SEE ALSO
    [Type::Tiny::Manual](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual/markdown).

    [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown), [Type::Utils](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AUtils/markdown), [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown), [Type::Coercion](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ACoercion/markdown).

    [Moose::Util::TypeConstraints](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AUtil%3A%3ATypeConstraints/markdown), [Mouse::Util::TypeConstraints](https://www.chedong.com/phpMan.php/perldoc/Mouse%3A%3AUtil%3A%3ATypeConstraints/markdown).

## AUTHOR
    Toby Inkster <<tobyink@cpan.org>>.

## COPYRIGHT AND LICENCE
    This software is copyright (c) 2013-2014, 2017-2021 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.

