# New Ticket Created by  Chris Fields 
# Please include the string:  [perl #58818]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=58818 >


When attempting to set up a simple clone method (awaiting .clone to be  
implemented), I noticed an odd issue where passing attributes directly  
to new() using self.attribute winds up binding to the original  
object's attribute.  However, using a temp variable works fine.   
Notably, the problem disappears if the attribute in question is typed  
('has Int $.a is rw;' fixes it).

class Foo {
     has $.a is rw;
     has $.b is rw;
     method clone_Foo {
         my $newval = self.b;
         return self.new(a => self.a, b => $newval);
     }
};

my $first = Foo.new(a => 1, b => 2);
say $first.a; # 1
say $first.b; # 2

my $second = $first.clone_Foo;
say $second.a; # 1
say $second.b; # 2

$second.a = 4;
$second.b = 8;

say $second.a; # 4
say $second.b; # 8
say $first.a;  # should be 1, is 4
say $first.b;  # 2 (correct)


chris

Reply via email to