Closed forwardever closed 10 years ago
Hmm, I will have to think about this one, I would sooner do something closer to Moose, like:
has $one;
has $two;
has $three;
submethod BUILDARGS ($_one, $_two, $_three) {
return +{ one => $_one, two => $_two, three => $_three }
}
Though that might get tricky since it is technically a class method.
okay, would be great to have something more customizable, thanks
have adjusted my first example, hope it makes more sense now
I need to finish up the traits stuff today, but I will keep thinking on this, but yeah, your example looks good.
@forwardever – I did some testing today and this works in the latest p5-mop
class Baz {
has $bar;
method new ($x) {
$self->next::method( bar => $x )
}
method bar { $bar }
}
{
my $baz = Baz->new( 10 );
isa_ok($baz, 'Baz');
is($baz->bar, 10, '... overriding new works');
}
It is not as nice as the BUILDARGS idea, but for now it does what you would want.
nice workaround, long term, would be nice to get something like this:
use mop;
class TestClass {
has $one;
has $two;
has $three;
has $hello;
method new (@params) {
$one = shift @params;
$two = shift @params;
$three = shift @params;
$self->next::method(@params);
}
method one {$one}
method two {$two}
method three {$three}
method hello {$hello}
}
my $test = TestClass->new(1, 2, 3, hello => 'world');
warn $test->one;
warn $test->two;
warn $test->three;
warn $test->hello;
which might not make sense, as $self is created in $self->next::method(@params)
yeah, this is a little tricky, but we are getting there
basically, I like Rubys initialize method: http://ruby.activeventure.com/usersguide/rg/objinitialization.html
Yeah, I am taking a similar approach for inheriting from non-mop classes, ... https://github.com/stevan/p5-mop-redux/blob/master/t/050-non-mop-integration/003-attributes-in-non-mop-inherited-class.t so I think something like this could be worked out.
For what it's worth, that's basically how BUILD
works in perl 6.
I think this issue is basically resolved (especially since @forwardever has finally come around to our way of thinking re: attribute visibility in subclasses).