Skip to content

Commit

Permalink
pairs, %hashes live
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Aug 11, 2010
1 parent d616f47 commit e2d76bb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
39 changes: 30 additions & 9 deletions SAFE.setting
Expand Up @@ -215,8 +215,8 @@ my class UInt { }
my class Bit { }
my class Instant { }
my class Duration { }
my class Enum is Cool { }
my class Bool is Enum {
my class EnumType is Cool { }
my class Bool is EnumType {
method Str() { self ?? "Bool::True" !! "Bool::False" }
method Bool() { self }
method ACCEPTS($) { self }
Expand Down Expand Up @@ -787,13 +787,6 @@ my class Array is List {
}
}
PRE-INIT {
Q:CgOp { (prog (rawsset Kernel.ArrayP (@ (l Array))) (null Variable)) };
Any.HOW.add-method("at-pos", anon method at-pos($ix) {
($ix == 0) ?? self !! die("Invalid index on non-list")
});
}
sub postcircumfix:<[ ]> is rawcall {
my $index ::= Q:CgOp { (pos 1) };
Expand Down Expand Up @@ -835,6 +828,16 @@ my class Hash {
}
}
PRE-INIT {
Q:CgOp { (prog (rawsset Kernel.ArrayP (@ (l Array)))
(rawsset Kernel.HashP (@ (l Hash)))
(null Variable)) };
Any.HOW.add-method("at-pos", anon method at-pos($ix) {
($ix == 0) ?? self !! die("Invalid index on non-list")
});
}
sub postcircumfix:<{ }> is rawcall {
my $key ::= Q:CgOp { (pos 1) };
Expand Down Expand Up @@ -958,4 +961,22 @@ sub _rxbind($C, @names, $fun, $k) {
});
}
my class Enum is Cool {
has $.key;
has $.value;
method kv() {
($.key, $.value);
}
method pairs() {
self.flat;
}
}
my class Pair is Enum {
}
sub infix:<< => >>($k, $v) { Pair.RAWCREATE("key", $k, "value", $v) }
{YOU_ARE_HERE}
17 changes: 16 additions & 1 deletion test.pl
Expand Up @@ -2,7 +2,7 @@

use Test;

plan 255;
plan 264;

ok 1, "one is true";
ok 2, "two is also true";
Expand Down Expand Up @@ -687,3 +687,18 @@
is bar(10,20).join("|"), "10|20", "2x overriding works";
is bar(10).join("|"), "10|9", "one value hits the right parameter";
}
{
is :foo.value, 'Bool::True', ":foo is true";
is :!foo.value, 'Bool::False', ":!foo is false";
is :foo<12>.value, '12', ":foo<12> is 12";
is :foo.key, 'foo', ":foo is foo";
is (foo => 1).key, 'foo', "foo => 1 keeps key";
is (foo => 1).value, '1', "foo => 1 keeps value";
is ("foo" => 1).key, 'foo', '"foo" => 1 keeps key';
is ("foo" => 1).value, '1', '"foo" => 1 keeps value';
my %hash;
ok %hash ~~ Hash, '%-vars are Hash';
}
33 changes: 0 additions & 33 deletions test2.pl
@@ -1,37 +1,4 @@
# vim: ft=perl6
use Test;

PRE-INIT { Q:CgOp { (prog (rawsset Kernel.HashP (@ (l Hash))) (null Variable)) } }
my class Enum is Cool {
has $.key;
has $.value;
method kv() {
($.key, $.value);
}
method pairs() {
self.flat;
}
}
my class Pair is Enum {
}
sub infix:<< => >>($k, $v) { Pair.RAWCREATE("key", $k, "value", $v) }

is :foo.value, 'Bool::True', ":foo is true";
is :!foo.value, 'Bool::False', ":!foo is false";
is :foo<12>.value, '12', ":foo<12> is 12";
is :foo.key, 'foo', ":foo is foo";

is (foo => 1).key, 'foo', "foo => 1 keeps key";
is (foo => 1).value, '1', "foo => 1 keeps value";
is ("foo" => 1).key, 'foo', '"foo" => 1 keeps key';
is ("foo" => 1).value, '1', '"foo" => 1 keeps value';

my %hash;
ok %hash ~~ Hash, '%-vars are Hash';

done-testing;

0 comments on commit e2d76bb

Please sign in to comment.