On Wed, Dec 09, 2009 at 12:16:32AM -0500, Austin Hastings wrote:
> >>But I'm curious if there's some P6 feature I've forgotten about
> >>(which I've forgotten most of them, excepting the rev number)
> >>that would let me do this without having to go too far away from
> >>the metal.
> 
> Coming at this from a different angle, C# offers syntactic sugar for
> getter/setter methods. This example of mine might be a candidate for
> a macro, depending on the language. But is this a p6 macro? Or is
> there some in-between that I just don't know about.

In Perl 6, getter and setter methods are generated automatically
for most attributes that are declared with a dot twigil.  Writing

    has $.xyz;

in a class declaration (note the dot twigil) automatically 
defines a getter method for the class that is similar to:

    multi method xyz() { $!xyz }

And writing 

    has $.xyz is rw;

automatically creates a getter/setter method that allows both
read and write access to the invocant's $!xyz attribute.

I've thought briefly about having NQP automatically create
getter methods as well, but for the moment it feels safer to
not do this and require them to be written explicitly.  Writing 
p6-like setter (rw) methods in NQP is a real pain at the moment,
because native Parrot doesn't have a convenient mechanism for
a method to return a reference to an attribute slot.  (In other 
words, there's not a good way in core Parrot to do the equivalent 
of  "foo() := $value;"  or  "$obj.method() := $value;" .)

Pm

Reply via email to