Skip to content

Instantly share code, notes, and snippets.

@salortiz
Last active February 8, 2016 13:22
Show Gist options
  • Save salortiz/db2efe401153b74c0b10 to your computer and use it in GitHub Desktop.
Save salortiz/db2efe401153b74c0b10 to your computer and use it in GitHub Desktop.
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
@salortiz
Copy link
Author

salortiz commented Feb 8, 2016

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.

@jnthn
Copy link

jnthn commented Feb 8, 2016

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