Re: Coercions and custom type parameters

[email protected] (Charles Alderman)
Newsgroups perl.moose
Message-ID <[email protected]>
----- Original Message -----
From: Yuval Kogman <[email protected]>
Sent: Thu, 24 Jul 2008 23:27:11 +0300
Re: Re: Coercions and custom type parameters

> 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]".
>


OK, I hadn't thought about it that way.  So, I can declare a subtype  
that looks parametrized, but it really isn't.

subtype 'ArrayRef[Int]' => as 'Str' => where { 1 };


>
> 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
>

Wow, I didn't know that was possible either.  You guys have thought of  
everything...

So, this is my custom collection, which is parameterizable:

   subtype 'BigArrayRef' => as 'ArrayRef' => where { scalar(@$_) > 1 };

   has 'my_big_arrayref' => ( is => 'rw', isa => 'BigArrayRef[Int]' );


Now, I can also create a pseudo parameterizable subtype, that does  
something like this:

   subtype 'BigArrayRef[Int] => as 'ArrayRef' => where {
       my $ar = $_;
       return unless scalar( @$ar ) > 1;
       for ( @$ar ) {
         return unless m/^ \d+ $/x;
       }
       return 1;
   }

Well what does this mean now:

has 'my_new_big_arrayref' => ( is => 'rw', isa => 'BigArrayRef[Int][Int]' );

I think I've just confused myself.  I'm going to sit down and play  
with this some more...

Thanks,
Charles Alderman
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.