> On 05 Jan 2016, at 19:34, David (via RT) <perl6-bugs-follo...@perl.org> wrote:
> 
> # New Ticket Created by  David 
> # Please include the string:  [perl #127172]
> # in the subject line of all future correspondence about this issue. 
> # <URL: https://rt.perl.org/Ticket/Display.html?id=127172 >
> 
> 
> I propose this patch so as to make private attributes user friendly
> just like public attributes. With this patch:
> 
> - rakudo will automatically generate private accessors when you
> declare a private attribute, in the same manner it does when you
> declare a public attribute.
> - it will not generate the private accessor should a private method
> with the same name exist.
> - 'is rw' and 'is readonly' now makes sense for private attributes.
> 
> The use case is as follows:
> 
> class MyClass {
>    has $!secret = 5;
> 
>    method sauce(MyClass $other) {
>        return self!secret() + $other!secret();
>    }
> }
> 
> say MyClass.new().sauce( MyClass.new() );
> 
> Instead of having to manually write a private accessor for $!secret,
> rakudo now does it for you - user friendly :-)

Yes, but at what overhead?

I’d rather see a module space solution in the form of a trait:

class MyClass {
    has $!secret is accessible-by-method = 5; # auto-created method secret { 
$!secret }
}

In most cases when I’v created classes, I want private attributes to be 
private.  If you really need to be able to access them for objects other than 
self, or from the outside, either make it an attribute with an accessor, or 
create your own accessor method.

To make it DRY, a trait would be best!


my 2c worth


Liz

Reply via email to