Re: Extending an attribute with a trait
[email protected] (Stevan Little)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
Dan,
Yes, there is, if you override the method
"legal_options_for_inheritance" in Moose::Meta::Attribute in your
trait so that it includes "foo" as well and that should do it.
- Stevan
On Jul 15, 2008, at 4:55 PM, Dan Harbin wrote:
> Stevan, other Moose-people,
>
> I'm having a problem using an attribute trait in a subclass. If I
> have an attribute in the superclass, then extend that attribute in the
> subclass by adding a trait, it complains about the behavior I'm adding
> with: Illegal inherited options => (foo). Since I made "foo"
> required, then I can't add the trait without it complaining: Attribute
> (foo) is required.
>
> The following code demonstrates this issue. Is there a workaround?
>
> -----------------------
>
> package Demo::Meta::Attribute::Trait::Foo;
> use Moose::Role;
>
> has foo => (
> is => 'rw',
> isa => 'Str',
> required => 1,
> );
>
> package Moose::Meta::Attribute::Custom::Trait::Foo;
> sub register_implementation { 'Demo::Meta::Attribute::Trait::Foo' }
>
> package MyClass;
> use Moose;
>
> has 'one' => (
> is => 'rw',
> isa => 'Str',
> traits => [qw/Foo/],
> foo => 'bar',
> );
>
> has 'two' => (
> is => 'rw',
> isa => 'Str',
> );
>
> package MySubClass;
> use Moose;
> extends 'MyClass';
>
> has '+two' => (
> traits => [qw/Foo/],
> foo => 'baz',
> );
>
> package main;
> use strict;
> use warnings;
>
> my $o = MySubClass->new();
>
> 1;