PetaMem has asked for the wisdom of the Perl Monks concerning the following question:
Fellow monks,
I'm trying to refactor some legacy code using a Moose'ish way. The old code has tons of codepaths to work with different types of data (objects may be strings, hashrefs, regexps, arrayrefs) and I thought using Moose unions would probably be the way to go.
Coercions are probably not the way to go, at least not in the general case, (the code may use coercions when e.g. 'consolidating' data structures).
In order to get a little bit more structure to the whole thing, I thought of an abstraction into two Moose classes: 'KeyAtom' and 'ValAtom', where KeyAtom may be of type 'Str | RegexpRef' and ValAtom of the types that are valid for KeyAtom plus 'HashRef | ArrayRef'.
Unfortunately, doing (shortened)Doesn't work. Of course, because KeyAtom naturally isn't the same as 'Str | RegexpRef'. So when the definition of ValAtom data type would have been.package KeyAtom; has 'data' => ( is => 'rw', isa => 'Str | RegexpRef', ); package ValAtom; extends 'KeyAtom'; has 'data' => ( is => 'rw', isa => 'KeyAtom | ArrayRef | HashRef', );
All works. However, true happiness doesn't come up, as this requires to manually maintain basically redundant information. So the core question is how to get the type definition in the inherited classes "do what I mean"?has 'data' => ( is => 'rw', isa => 'Str | RegexpRef | ArrayRef | HashRef', );
Bye
PetaMem All Perl: MT, NLP, NLU
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Moose "unions" and inheritance
by tobyink (Canon) on Nov 30, 2012 at 10:27 UTC | |
by remiah (Hermit) on Nov 30, 2012 at 11:22 UTC | |
by tobyink (Canon) on Nov 30, 2012 at 12:24 UTC | |
by remiah (Hermit) on Dec 01, 2012 at 00:22 UTC | |
by PetaMem (Priest) on Nov 30, 2012 at 11:42 UTC | |
by tobyink (Canon) on Nov 30, 2012 at 12:06 UTC | |
by PetaMem (Priest) on Nov 30, 2012 at 13:49 UTC | |
Re: Moose "unions" and inheritance
by tobyink (Canon) on Nov 30, 2012 at 12:30 UTC |