Skip to content

Commit

Permalink
Allow for initializations of "is List" attributes
Browse files Browse the repository at this point in the history
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 b537ce2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Perl6/Metamodel/BUILDPLAN.nqp
Expand Up @@ -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;
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/Perl6/World.nqp
Expand Up @@ -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')
)
)
);
}
Expand Down Expand Up @@ -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')
)
)
);
}
Expand Down

0 comments on commit b537ce2

Please sign in to comment.