1Type::Tiny::Manual::UsiUnsgeWritChoMnotor(i3b)uted PerlTDyopceu:m:eTnitnayt:i:oMnanual::UsingWithMoo(3)
2
3
4
6 Type::Tiny::Manual::UsingWithMoo - how to use Type::Tiny and
7 Type::Library with Moo
8
10 {
11 package Person;
12
13 use Moo 1.006000;
14 use Sub::Quote qw( quote_sub );
15 use Types::Standard qw( Str Int );
16
17 has name => (
18 is => "ro",
19 isa => Str,
20 );
21
22 my $PositiveInt = Int
23 -> where( quote_sub '$_ > 0' )
24 -> plus_coercions( Int, sub { abs $_ } );
25
26 has age => (
27 is => "rwp",
28 isa => $PositiveInt,
29 coerce => 1,
30 );
31
32 sub get_older {
33 my $self = shift;
34 my ($years) = @_;
35 $PositiveInt->assert_valid($years);
36 $self->_set_age($self->age + $years);
37 }
38 }
39
41 Type::Tiny is tested with Moo 1.001000 and above.
42
43 Type::Tiny overloads "&{}". Moo supports using objects that overload
44 "&{}" as "isa" constraints, so Type::Tiny objects can directly be used
45 in "isa".
46
47 Moo prior to 1.006000 doesn't support "coerce => 1", instead requiring
48 a coderef to use as a coercion. However, Type::Tiny can provide you
49 with a suitable coderef to use (actually an object that overloads
50 "&{}"). Just use:
51
52 has age => (
53 is => "rwp",
54 isa => $PositiveInt,
55 coerce => $PositiveInt->coercion,
56 );
57
58 If you can upgrade to the latest Moo, and use "coerce => 1" you'll have
59 a lot more fun though. :-)
60
61 Type::Tiny hooks into Moo's HandleMoose interface to ensure that type
62 constraints get inflated to Moose type constraints if and when Moo
63 inflates your class to a full Moose class.
64
65 Optimization
66 The usual advice for optimizing type constraints applies: use type
67 constraints which can be inlined whenever possible, and define
68 coercions as strings rather than coderefs.
69
70 Upgrading to Moo 1.002000 or above should provide a slight increase in
71 speed for type constraints, as it allows them to be inlined into
72 accessors and constructors.
73
74 If creating your own type constraints using "Type::Tiny->new", then
75 consider using Sub::Quote to quote the coderef; this allows you to take
76 advantage of inlining without having to write your own inlining
77 routines.
78
79 See also Type::Tiny::Manual::Optimization.
80
82 For examples using Type::Tiny with Moo see the SYNOPSIS sections of
83 Type::Tiny and Type::Library, and the Moo integration tests
84 <https://github.com/tobyink/p5-type-
85 tiny/tree/master/t/30-integration/Moo> in the test suite.
86
88 Toby Inkster <tobyink@cpan.org>.
89
91 This software is copyright (c) 2013-2014, 2017-2019 by Toby Inkster.
92
93 This is free software; you can redistribute it and/or modify it under
94 the same terms as the Perl 5 programming language system itself.
95
97 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
98 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
99 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
100
101
102
103perl v5.28.1 2019-01-08Type::Tiny::Manual::UsingWithMoo(3)