> I'd either like a way to more cleanly extend the default accessor's > assignment behavior down the road (i.e. by just writing a new name() method, > not by hacking away at STORE traits and adding private worker subs) or a way > to auto-generate the slightly more "boring" default accessor shown above.
There *is* a way to do the latter. In A12, Larry implies that the declarators in the body of a class definition are actually method calls on an instance of MetaClass:
http://www.perl.com/pub/a/2004/04/16/a12.html?page=3#use_of_classes http://www.perl.com/pub/a/2004/04/16/a12.html?page=19#new_grammatical_categories
So, presumably, by defining a new C<scope_declarator:has>, you would be able to override the default accessor-generating behaviour.
The least scary way to do this would be to encapsulate it in a trait that is applied to (and has its way with ;- the class declaration:
class Dog is getset {
has $.name is rw;
...
}or perhaps is applied instead to individual attribute declarations:
class Dog {
has $.name is getset;
...
}
Alternatively, you could just *cheat* and define a macro (putting it in a lexically-scoped module for convenience and safety):
module GetSet;
macro getset is export(:MANDATORY)
is parsed(rx:words/ <?Perl6.type>? <?Perl6.attr_var>/)
($attr)
{
my $accessor = substr $attr{attr_var}, 2;
return "method set_$accessor ($attr{type} $attr{$attr_var}) {} "
~ "method get_$accessor () { return $attr{attr_var} } "
~ "has $attr{type} $attr{attr_var}"
;
}and then:
class Dog {
use GetSet;
getset $.name;
...
}Damian
