in reply to Moose: Problems subtyping a coerced subtype
Moose won't do coersion unless you request it, so, at the least, you would need:
has file => ( isa => 'ExistingFile', is => 'ro', required => 1, coerce + => 1 );
However, that doesn't work for me either, complaining:
You cannot coerce an attribute (file) unless its type (ExistingFile) h +as a coercion at /tmp/test.pl line 26
Apparently, coersion is not inherited by subtypes by design, so you will need to duplicate the coersion.
subtype 'ExistingFile' => as 'AFile' => where { say "In subtype where clause: $_[0]"; -f $_[0] }, message { "$_[0] does not exist" }; coerce 'ExistingFile' => from 'Str' => via { file($_) }; has file => ( isa => 'ExistingFile', is => 'ro', required => 1, coerce + => 1 );
Note: You can reduce the code duplicationg using:
for my $ftype (qw/ AFile ExistingFile /) { coerce $ftype => from 'Str' => via { file($_) }; }
Good Day,
Dean
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Moose: Problems subtyping a coerced subtype
by BaldManTom (Friar) on Jan 21, 2015 at 18:04 UTC | |
by kennethk (Abbot) on Jan 21, 2015 at 19:54 UTC | |
by BaldManTom (Friar) on Jan 22, 2015 at 13:10 UTC |