diff options
author | Toby Inkster <mail@tobyinkster.co.uk> | 2022-09-22 10:25:56 +0100 |
---|---|---|
committer | Toby Inkster <mail@tobyinkster.co.uk> | 2022-09-22 10:25:56 +0100 |
commit | aa8f2a659d26b826047eb7b7a17b2038e4f6db68 (patch) | |
tree | 4653ae35940ede0830841c2b7a4218179637982e /lib | |
parent | 178a8090e9171b9831dea0090451e85ee334545f (diff) |
Document usage with Class::Plain, more Class::Plain tests
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Type/Tiny/Manual/UsingWithOther.pod | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/Type/Tiny/Manual/UsingWithOther.pod b/lib/Type/Tiny/Manual/UsingWithOther.pod index 3e5c0238..bd727a7d 100644 --- a/lib/Type/Tiny/Manual/UsingWithOther.pod +++ b/lib/Type/Tiny/Manual/UsingWithOther.pod @@ -121,6 +121,64 @@ in production as it slows down C<UNIVERSAL::isa> globally. $bob->name("Robert"); # okay $bob->name([]); # dies +=head2 Class::Plain + +There is not currently a high level of integration, but here's a quick +example of type checking attributes in the constructor. + +If any of your accessors are C<< :rw >> then you would also need to +add type checks to those. + + use Class::Plain; + + class Point { + use Types::Common -types, -sigs; + + field x :reader; + field y :reader; + + signature_for new => ( + method => !!1, + bless => !!0, + named => [ + x => Int, + y => Int, + ], + ); + + method as_arrayref () { + return [ $self->x, $self->y ]; + } + } + +The following signature may also be of interest: + + signature_for new => ( + method => !!1, + multiple => [ + { + named => [ + x => Int, + y => Int, + ], + bless => !!0, + }, + { + positional => [ Int, Int ], + goto_next => sub { + my ( $class, $x, $y ) = @_; + return ( $class, { x => $x, y => $y } ), + }, + }, + ], + ); + +This would allow your class to be instantiated using any of the following: + + my $point11 = Point->new( { x => 1, y => 1 } ); + my $point22 = Point->new( x => 2, y => 2 ); + my $point33 = Point->new( 3, 3 ); + =head1 NEXT STEPS Here's your next step: |