From b537ce2e5ac33304a63c50b1d46876bca0e907ef Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Mon, 24 Sep 2018 19:34:38 +0200 Subject: [PATCH] Allow for initializations of "is List" attributes This commit makes it possible to assign to attributes marked as "is List", so that you can create Listy attributes that are immutable. class A { has @.foo is List } my $a = A.new( foo => (1,2,3) ); dd $a; # A.new(foo => (1, 2, 3)) $a.a[1] = 666; # cannot change immutable List Adapt the workaround message for R#1226. --- src/Perl6/Metamodel/BUILDPLAN.nqp | 6 ++++-- src/Perl6/World.nqp | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Perl6/Metamodel/BUILDPLAN.nqp b/src/Perl6/Metamodel/BUILDPLAN.nqp index 87ce4fe0cf8..5905f5ca65e 100644 --- a/src/Perl6/Metamodel/BUILDPLAN.nqp +++ b/src/Perl6/Metamodel/BUILDPLAN.nqp @@ -50,9 +50,11 @@ role Perl6::Metamodel::BUILDPLAN { workaround => "Create/Adapt TWEAK method in class " ~ $obj.HOW.name($obj) - ~ ", e.g:\n method TWEAK(:" + ~ ", e.g:\n\n method TWEAK() \{\n " ~ $_.name - ~ ' = (initial values)) { }' + ~ " := (initial values) unless " + ~ $_.name + ~ ";\n }" ).throw; } } diff --git a/src/Perl6/World.nqp b/src/Perl6/World.nqp index c4034b38eb9..03b316deced 100644 --- a/src/Perl6/World.nqp +++ b/src/Perl6/World.nqp @@ -3285,11 +3285,16 @@ class Perl6::World is HLL::World { my $sigil := nqp::substr(nqp::atpos($task,2),0,1); -# nqp::getattr(self,Foo,'$!a').STORE(%init.AT-KEY('a')) +# nqp::getattr(self,Foo,'$!a').STORE(%init.AT-KEY('a'), :initialize) if $sigil eq '@' || $sigil eq '%' { $if.push( QAST::Op.new( :op, :name, - $getattr, $value + $getattr, $value, QAST::WVal.new( + :value($!w.find_symbol( + ['Bool','True'], :setting-only + )), + :named('initialize') + ) ) ); } @@ -3374,11 +3379,16 @@ class Perl6::World is HLL::World { !! QAST::WVal.new(:value(nqp::atpos($task,3))); my $sigil := nqp::substr(nqp::atpos($task,2),0,1); -# nqp::getattr(self,Foo,'$!a').STORE($code(self,nqp::getattr(self,Foo,'$!a'))) +# nqp::getattr(self,Foo,'$!a').STORE($code(self,nqp::getattr(self,Foo,'$!a')), :initialize) if $sigil eq '@' || $sigil eq '%' { $unless.push( QAST::Op.new( :op, :name, - $getattr, $initializer + $getattr, $initializer, QAST::WVal.new( + :value($!w.find_symbol( + ['Bool','True'], :setting-only + )), + :named('initialize') + ) ) ); }