A question about role-private attributes

2008-11-09 Thread [EMAIL PROTECTED]
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:

Cmy $!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 Cmy $spleen would give us the third
interpretation. But I do not know what syntax would result in a role-
private class attribute.

Thanks,
  Abhijit



Re: A question about role-private attributes

2008-11-09 Thread Jonathan Worthington

[EMAIL PROTECTED] wrote:

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:

Cmy $!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.
===
  
It's an attribute - thus it's per instance of the class(es) the role was 
composed into. The lexical stuff is just about the visibility of the 
attribute, not saying it's like a lexical variable in that it's one of 
them existing for the block (which isn't even true for blocks in 
general, since there can be closures).



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.
  
I believe it would print one three. I expected this would work in Rakudo 
too - will look into why it doesn't.


Thanks,

Jonathan