# phpman > info > Type::Tiny::Manual::UsingWithMoose

[Type::Tiny::Manual::UsUseriContributed](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3AUsUseriContributed/markdown))[Type::Tiny::Manual](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual/markdown)::[UsingWithMoose(3pm)](https://www.chedong.com/phpMan.php/man/UsingWithMoose/3pm/markdown)

NAME
       [Type::Tiny::Manual::UsingWithMoose](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3AUsingWithMoose/markdown) - how to use [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) with Moose

MANUAL
       First read [Type::Tiny::Manual::Moo](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3AMoo/markdown), [Type::Tiny::Manual::Moo2](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3AMoo2/markdown), and
       [Type::Tiny::Manual::Moo3](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3AMoo3/markdown). Everything in those parts of the manual
       should work exactly the same in Moose.

       This part of the manual will focus on Moose-specifics.

   Why Use [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) At All?
       Moose does have a built-in type constraint system which is fairly
       convenient to use, but there are several reasons you should consider
       using [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) instead.

       o   [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) type constraints will usually be faster than Moose
           built-ins.  Even without [Type::Tiny::XS](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AXS/markdown) installed, [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown)
           usually produces more efficient inline code than Moose. Coercions
           will usually be a lot faster.

       o   [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) provides helpful methods like "where" and
           "plus_coercions" that allow type constraints and coercions to be
           easily tweaked on a per-attribute basis.

           Something like this is much harder to do with plain Moose types:

             has name => (
               is      => "ro",
               isa     => Str->plus_coercions(
                 ArrayRef[Str], sub { join " ", @$_ },
               ),
               coerce  => 1,
             );

           Moose tends to encourage defining coercions globally, so if you
           wanted one Str attribute to be able to coerce from ArrayRef[Str],
           then all Str attributes would coerce from ArrayRef[Str], and they'd
           all do that coercion in the same way. (Even if it might make sense
           to join by a space in some places, a comma in others, and a line
           break in others!)

       o   [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) provides automatic deep coercions, so if type Xyz has a
           coercion, the following should "just work":

             isa xyzlist => ( is => 'ro', isa => ArrayRef[Xyz], coerce => 1 );

       o   [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) offers a wider selection of built-in types.

       o   By using [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown), you can use the same type constraints and
           coercions for attributes and method parameters, in Moose and non-
           Moose code.

   [Type::Utils](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AUtils/markdown)
       If you've used [Moose::Util::TypeConstraints](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AUtil%3A%3ATypeConstraints/markdown), you may be accustomed to
       using a DSL for declaring type constraints:

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

         subtype 'Natural',
           as 'Int',
           where { $_ > 0 };

       There's a module called [Type::Utils](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AUtils/markdown) that provides a very similar DSL
       for declaring types in [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown)-based type libraries.

         package [My::Types](https://www.chedong.com/phpMan.php/perldoc/My%3A%3ATypes/markdown) {
           use [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown) -base;
           use [Type::Utils](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AUtils/markdown);
           use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw( Int );

           declare 'Natural',
             as Int,
             where { $_ > 0 };
         }

       Personally I prefer the more object-oriented way to declare types
       though.

       Since [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown) 1.012, a shortcut has been available for importing
       [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown) and [Type::Utils](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AUtils/markdown) at the same time:

         package MyType {
           use [Type::Library](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ALibrary/markdown) -base, -utils;

           ...;
         }

       In Moose you might also declare types like this within classes and
       roles too.  Unlike Moose, [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) doesn't keep types in a single
       global flat namespace, so this doesn't work quite the same with
       [Type::Utils](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AUtils/markdown). It still creates the type, but it doesn't store it in any
       type library; the type is returned.

         package [My::Class](https://www.chedong.com/phpMan.php/perldoc/My%3A%3AClass/markdown) {
           use Moose;
           use [Type::Utils](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3AUtils/markdown);
           use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw( Int );

           my $Natural =          # store type in a variable
             declare 'Natural',
             as Int,
             where { $_ > 0 };

           has number => ( is => 'ro', isa => $Natural );
         }

       But really, isn't the object-oriented way cleaner?

         package [My::Class](https://www.chedong.com/phpMan.php/perldoc/My%3A%3AClass/markdown) {
           use Moose;
           use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw( Int );

           has number => (
             is   => 'ro',
             isa  => Int->where('$_ > 0'),
           );
         }

   [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) and [MooseX::Types](https://www.chedong.com/phpMan.php/perldoc/MooseX%3A%3ATypes/markdown)
       [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) should be a drop-in replacement for [MooseX::Types](https://www.chedong.com/phpMan.php/perldoc/MooseX%3A%3ATypes/markdown).  And
       [Types::Common::Numeric](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3ACommon%3A%3ANumeric/markdown) and [Types::Common::String](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3ACommon%3A%3AString/markdown) should easily replace
       [MooseX::Types::Common::Numeric](https://www.chedong.com/phpMan.php/perldoc/MooseX%3A%3ATypes%3A%3ACommon%3A%3ANumeric/markdown) and [MooseX::Types::Common::String](https://www.chedong.com/phpMan.php/perldoc/MooseX%3A%3ATypes%3A%3ACommon%3A%3AString/markdown).

       That said, if you do with to use a mixture of [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) and
       [MooseX::Types](https://www.chedong.com/phpMan.php/perldoc/MooseX%3A%3ATypes/markdown), they should fit together pretty seamlessly.

         use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw( ArrayRef );
         use [MooseX::Types::Common::Numeric](https://www.chedong.com/phpMan.php/perldoc/MooseX%3A%3ATypes%3A%3ACommon%3A%3ANumeric/markdown) qw( PositiveInt );

         # this should just work
         my $list_of_nums = ArrayRef[PositiveInt];

         # and this
         my $list_or_num = ArrayRef | PositiveInt;

   "-moose" Import Parameter
       If you have read this far in the manual, you will know that this is the
       usual way to import type constraints:

         use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw( Int );

       And the "Int" which is imported is a function that takes no arguments
       and returns the Int type constraint, which is a blessed object in the
       [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) class.

       [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) mocks the [Moose::Meta::TypeConstraint](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AMeta%3A%3ATypeConstraint/markdown) API so well that most
       Moose and MooseX code will not be able to tell the difference.

       But what if you need a real [Moose::Meta::TypeConstraint](https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AMeta%3A%3ATypeConstraint/markdown) object?

         use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) -moose, qw( Int );

       Now the "Int" function imported will return a genuine native Moose type
       constraint.

       This flag is mostly a throwback from when [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) native objects
       didn't directly work in Moose. In 99.9% of cases, there is no reason to
       use it and plenty of reasons not to. (Moose native type constraints
       don't offer helpful methods like "plus_coercions" and "where".)

   "moose_type" Method
       Another quick way to get a native Moose type constraint object from a
       [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) object is to call the "moose_type" method:

         use [Types::Standard](https://www.chedong.com/phpMan.php/perldoc/Types%3A%3AStandard/markdown) qw( Int );

         my $tiny_type   = Int;
         my $moose_type  = $tiny_type->moose_type;

       Internally, this is what the "-moose" flag makes imported functions do.

NEXT STEPS
       Here's your next step:

       o   [Type::Tiny::Manual::UsingWithMouse](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3AUsingWithMouse/markdown)

           How to use [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) with Mouse, including the advantages of
           [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) over built-in type constraints, and Mouse-specific
           features.

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.

perl v5.32.1                      2021-[Type::Tiny::Manual](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual/markdown)::[UsingWithMoose(3pm)](https://www.chedong.com/phpMan.php/man/UsingWithMoose/3pm/markdown)
