Re: Coercions and custom type parameters

[email protected] (Yuval Kogman)
Newsgroups perl.moose
Message-ID <[email protected]>
This has been brought up before, the short story is 'coerce => 1'
may introduce action at a distance, so we decided that if at all
this should be 'deep_coerce => 1'.

At any rate, this is a little trickier than it sounds, but if Stevan
approves deep_coerce => 1 feel free to commit this as a todo test
and start hacking away.

Regards,
Yuval

On Thu, Jul 24, 2008 at 15:20:13 -0400, Charles Alderman wrote:
> Hello Moose,
>
> I guess I have an enhancement idea/request.
>
> I have a parametrized ArrayRef[] of a custom type, I'd like my coercion on 
> that type to work for any of the values in the collection.  HashRef[]s 
> should work too.  Maybe Maybe[]s, but not in my example below.
>
> Would this be worthwhile or viable?  If so, I'd be willing to attempt a 
> patch...
>
> Thanks,
> Charles Alderman
>
>
> Here's a test:
>
> ----
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use Test::More tests => 2;
> use Test::Exception;
>
> {
>
>   package Foo;
>   use Moose;
>   use Moose::Util::TypeConstraints;
>
>   enum 'Foo::Values' => qw{ Foo Bar Baz None };
>   coerce 'Foo::Values' => from 'Undef' => via { 'None' };
>
>   has 'array_of_foo' => (
>     is     => 'rw',
>     isa    => 'ArrayRef[Foo::Values]',
>     coerce => 1,
>   );
>
> }
>
> {
>   my $foo = Foo->new();
>
>   my @ok_values = ( 'Foo', 'Bar', 'Baz', 'None' );
>   my @coerced_values = ( 'Foo', 'Bar', undef, 'None' );
>
>   lives_ok {
>     $foo->array_of_foo( \@ok_values );
>   }
>   '... setting array with ok values';
>
>   lives_ok {
>     $foo->array_of_foo( \@coerced_values );
>   }
>   '... setting array with coerced values';
>
> }
>

-- 
  Yuval Kogman <[email protected]>
http://nothingmuch.woobling.org  0xEBD27418
signature.asc (application/pgp-signature, 197 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEARECAAYFAkiI2gkACgkQVCwRwOvSdBiexQCgliU8z1R5vyqW2IPtH+1afgpb
zq4AnRdJ6dZpl2Ql2WZvnwU7vhVmDk8e
=RiZI
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.