2010/1/19 Hans Dieter Pearcey <h...@pobox.com>: > Excerpts from Dermot's message of Tue Jan 19 08:28:55 -0500 2010: >> 2010/1/19 Hans Dieter Pearcey <h...@pobox.com>: >> > subtype 'Whatever', as enum([ qw(foo bar baz) ]), ...; > ^^^^^^^^^^^^^^^^^^^^^^^^^ > >> So the answer is use 'as'. > > Yes, that is *part* of the answer. Look at the rest of the example. > >> subtype 'IndexType' >> => as enum( $_, qw[foo bar baz]), > > This only works accidentally. > > You are taking whatever is in $_ (maybe undef, maybe .. who knows) and > declaring an enum with that name. This form of enum() happens to also return > that type name, but that's an implementation detail and not documented, so you > shouldn't depend on it. > > If you're unlucky, some day $_ will contain "Int" and all your integer > attributes will suddenly start throwing error messages about how they don't > match "foo", "bar", or "baz".
my $indexer = MyApp::Local::Indexer->new(index_type=>'foobarquin); I thought $_ was the value passed by the constructor arguments. In this case, $_ ='foobarquin' and it would been used to cmp against the strings in the array. > Did you read the docs for enum() in Moose::Util::TypeConstraints? I did but I'm struggling. I can see from the 2nd (arrayref) enum reference that the form: enum ($name, \...@values) returns an named enum. Which is like the example "enum 'RGBColors' => qw(red green blue);" So my enum($_, \...@array) would have created a subtype who's name was whatever was in $_. Ok, if that's true, that's not what I intended. For me the issue was turning this enum 'IndexType' => qw(foo bar baz); has 'index_type' => (isa => 'IndexType'); into subtype 'IndexType', => as enum( [qw(foo bar baz)] ), => message {'Wrong type'}; has 'index_type' => (isa => 'IndexType'); now the above works. Is this a correct implementation? Could this have been done with a named enum? Would it make sense to create a named enum type inside a subtype? The docs say that you can construct constraints with subtype 'Name' => as 'Parent' => .... Are these 'Parents' objects the class_type, duck_type and enum as well as other Classes you might want to harness? Thanks again, Dp. Thanks. Dp.