On 04/15/2010 02:41 PM, Chris Prather wrote:
On Thu, Apr 15, 2010 at 1:20 PM, Sir Robert Burbridge
<rburb...@cisco.com>  wrote:

Ahh, thanks.  So typing is basically nomination of classes into a program's
cosmology.  That's super helpful.

Thanks again,
Types work on things other than classes as well. It's more of a way of
saying "This arrangement of Data is special. Here's a name for it."

For example you can define a type that has nothing to do with a class at all:

subtype PersonRecord =>  as HashRef =>  where { exists $_->{first_name}
&&  exists $_->{last_name} };

This would match:  { first_name =>  'Cory', last_name =>  'Watson' }

but wouldn't match:

class Person::Record { has first  =>  ( accessor =>  'first_name' ); has
last =>  ( accessor =>  'last_name' ); }

...

This distinction becomes important when you get into things like
coercions. You can then say something like:

coerce 'Person::Record' =>  from PersonRecord =>  via {
Person::Record->new(first = $_->{first_name}, last =>  $_->{last_name})
};

and create objects from hashes with a minimum of fuss. There are also
things like MatchOnType in Moose::Util::TypeConstraints and other
tools that come into play once you have a good grasp of the Typing
system.

-Chris
Ahh, that's pretty clever. I didn't quite catch that from my first read of the docs, but I can see it now. I'll have to fiddle with it a while.


Thanks again =)

-Sir

Reply via email to