Skip to content
Permalink
Browse files

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.
  • Loading branch information...
lizmat committed Sep 24, 2018
1 parent 2bdf2b3 commit b537ce2e5ac33304a63c50b1d46876bca0e907ef
Showing with 18 additions and 6 deletions.
  1. +4 −2 src/Perl6/Metamodel/BUILDPLAN.nqp
  2. +14 −4 src/Perl6/World.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;
}
}
@@ -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<callmethod>, :name<STORE>,
$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<callmethod>, :name<STORE>,
$getattr, $initializer
$getattr, $initializer, QAST::WVal.new(
:value($!w.find_symbol(
['Bool','True'], :setting-only
)),
:named('initialize')
)
)
);
}

0 comments on commit b537ce2

Please sign in to comment.
You can’t perform that action at this time.