=pod

=encoding utf-8

=head1 NAME

Type::Tiny::Manual::UsingWithMoo - how to use Type::Tiny and Type::Library with Moo

=head1 SYNOPSIS

   {
      package Person;
      
      use Moo 1.006000;
      use Sub::Quote qw( quote_sub );
      use Types::Standard qw( Str Int );
      
      has name => (
         is      => "ro",
         isa     => Str,
      );
      
      my $PositiveInt = Int
         -> where( quote_sub '$_ > 0' )
         -> plus_coercions( Int, sub { abs $_ } );
      
      has age => (
         is      => "rwp",
         isa     => $PositiveInt,
         coerce  => 1,
      );
      
      sub get_older {
         my $self = shift;
         my ($years) = @_;
         $PositiveInt->assert_valid($years);
         $self->_set_age($self->age + $years);
      }
   }

=head1 DESCRIPTION

Type::Tiny is tested with L<Moo> 1.001000 and above.

Type::Tiny overloads C<< &{} >>. Moo supports using objects that overload
C<< &{} >> as C<isa> constraints, so Type::Tiny objects can directly be used
in C<isa>.

Moo prior to 1.006000 doesn't support C<< coerce => 1 >>, instead
requiring a coderef to use as a coercion. However, Type::Tiny can
provide you with a suitable coderef to use (actually an object that
overloads C<< &{} >>). Just use:

   has age => (
      is      => "rwp",
      isa     => $PositiveInt,
      coerce  => $PositiveInt->coercion,
   );

If you can upgrade to the latest Moo, and use C<< coerce => 1 >> you'll
have a lot more fun though. :-)

Type::Tiny hooks into Moo's HandleMoose interface to ensure that type
constraints get inflated to Moose type constraints if and when Moo inflates
your class to a full Moose class.

=head2 Optimization

The usual advice for optimizing type constraints applies: use type constraints
which can be inlined whenever possible, and define coercions as strings rather
than coderefs.

Upgrading to Moo 1.002000 or above should provide a slight increase in speed
for type constraints, as it allows them to be inlined into accessors and
constructors.

If creating your own type constraints using C<< Type::Tiny->new >>, then
consider using L<Sub::Quote> to quote the coderef; this allows you to take
advantage of inlining without having to write your own inlining routines.

See also L<Type::Tiny::Manual::Optimization>.

=head1 SEE ALSO

For examples using Type::Tiny with L<Moo> see the SYNOPSIS sections of
L<Type::Tiny> and L<Type::Library>, and the
L<Moo integration tests|https://github.com/tobyink/p5-type-tiny/tree/master/t/30-integration/Moo>
in the test suite.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

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

=head1 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.

=cut
