Coercions and custom type parameters

2008-07-24 Thread Charles Alderman

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( [EMAIL PROTECTED] );
  }
  '... setting array with ok values';

  lives_ok {
$foo-array_of_foo( [EMAIL PROTECTED] );
  }
  '... setting array with coerced values';

}



Re: Coercions and custom type parameters

2008-07-24 Thread Yuval Kogman
On Thu, Jul 24, 2008 at 16:16:28 -0400, Charles Alderman wrote:
 I guess that if you're trying to prevent this as an action at a distance, 
 'deep_coerce' wouldn't really be acceptable either.  Wouldn't that just be 
 a more explicit warning of an action at a distance?

Declaring an attribute with a parameterized type:

has foo = (
isa = ArrayRef[Foo],
coerce = 1,
);

has a specific behavior right now, it enables only the coercions on
the type ArrayRef[Foo].

If this started coercing using 'Foo's coercions we break
compatibility, and deining a coercion on 'Foo' affects types that
parametrize on it.

deep_coerce is different because:

a. it doesn't break existing code
b. it's explicit about the fact that it's accepting coercions
from the type parameter, so a developer would know to look
there, instead of looking for coercions on 'ArrayRef[Foo]'.

 If moose type constraints become too complicated, I guess at some point, a 
 designer/developer just needs to turn the parametrized type into its own 
 class.  So, maybe I'm answering my own question here.

Custom parametrized types could still make sense though:

MyCustomType[Foo]

is a useful construct for custom container types, for functors, etc
etc, so deep_coerce *is* a useful property to have

 At any rate, I'll look more at the mailing list archives...

I think this was only discussed on IRC actually

-- 
  Yuval Kogman [EMAIL PROTECTED]
http://nothingmuch.woobling.org  0xEBD27418