-
-
Save salortiz/db2efe401153b74c0b10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Test; | |
use nqp; | |
plan(17); | |
class Foo { | |
has Mu $!storage1; | |
has Mu $!storage2; | |
has Mu $!storage3; | |
method check(Foo:D:) { | |
# storage1 | |
ok not nqp::attrinited(self, Foo, '$!storage1'), 'Not initted'; | |
ok not nqp::defined($!storage1), 'Not storage1 defined'; | |
ok not nqp::isnull($!storage1), 'But NOT null'; | |
nqp::bindattr(self, Foo, '$!storage1', nqp::hash); | |
ok so nqp::attrinited(self, Foo, '$!storage1'), 'Now initted'; | |
ok so nqp::defined($!storage1), 'Initted and defined'; | |
ok so $!storage1.DEFINITE, 'DEFINITE'; | |
# Some identity tests, irrelevant for this tests | |
#ok not nqp::istype($!storage1, Hash), 'Not Hash'; | |
#ok so $!storage1 ~~ Hash, 'Is hash-is'; | |
#ok not $!storage1, 'But false (empty)'; | |
# Now with $!storage2 | |
# See the effect of defined test over attrinited | |
ok not nqp::defined($!storage2), 'storage2 not defined'; | |
# The mention above initialize it: | |
ok nqp::attrinited(self, Foo, '$!storage2'), 'But attrinitied'; | |
ok not nqp::defined($!storage2), 'Undefined yet'; | |
ok not $!storage2.DEFINITE, '!DEFINITE'; | |
ok nqp::istype($!storage2, Mu), 'Is MU'; | |
ok not nqp::isnull($!storage2), 'Not null'; | |
# Now with $!storage3 | |
# See the effect of isnull over attrinited | |
ok not nqp::attrinited(self, Foo, '$!storage3'), 'storage3 NOT attrinitied'; | |
# An naive test: | |
ok not nqp::isnull($!storage3), 'not null'; | |
ok nqp::attrinited(self, Foo, '$!storage3'), 'But NOW attrinitied'; | |
ok not nqp::defined($!storage3), 'Undefined yet'; | |
} | |
method create(Foo:U:) { | |
my \f = nqp::create(Foo); | |
ok nqp::defined(f), 'Obj created'; | |
f.check(); | |
} | |
} | |
Foo.create(); | |
# vim set ft=perl6 |
Note that when accessing $!storage you are doing an nqp::getattr. That vivifies a Perl 6 Scalar on first access. It doesn't matter what operation you do on the result, you already brought the attribute to live with nqp::getattr.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This test shows that nqp::defined and even nqp::isnull changes the state of nqp::attrinited, a function that was discussed in IRC for optimization work.