I am trying to understand the following small portion from S12, and it
seems slightly ambiguous to me:

======= from S12:
You may wish to declare an attribute that is hidden even from the
class; a completely private role attribute may be declared like this:

    C<my $!spleen;>

The name of such a private attribute is always considered lexically
scoped. If a role declares private lexical items, those items are
private to the role due to the nature of lexical scoping.
=======

I was unable to figure out what this piece of code would print:

====
role A {
        my $!spleen;
        method set_spleen($x) { $!spleen = $x; return self }
        method say_spleen() { say  $!spleen }
}

class B does A {}
class C does A {}
B.new.set_spleen(3).say_spleen();
B.new.say_spleen();
C.new.say_spleen();

======

How many 3s will that print? Just a single 3 would indicate that every
instance of every class that composed A has its own copy. If 3 is
printed twice, that would indicate that not every instance but every
class has its own copy. All three 3s will indicate only a single copy
for the entire role.

My guess is that the first interpretation was intended. Is that guess
correct? I would also guess that C<my $spleen> would give us the third
interpretation. But I do not know what syntax would result in a role-
private class attribute.

Thanks,
  Abhijit

Reply via email to