Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #155 from lizmat/hashwithtypeandof
.hash now accepts optional :type and :of named parameters
  • Loading branch information
Carl Mäsak committed May 20, 2013
2 parents 8d2ec91 + 61db0e0 commit 14e7d74
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/core/Any.pm
Expand Up @@ -2,6 +2,7 @@ my class MapIter { ... }
my class Range { ... }
my class X::Bind::Slice { ... }
my class X::Bind::ZenSlice { ... }
my $default= []; # so that we can check passing of parameters to ".hash"

my class Any {
multi method ACCEPTS(Any:D: Mu \a) { self === a }
Expand All @@ -17,7 +18,21 @@ my class Any {
method uniq() { self.list.uniq }
method infinite() { Mu }
method flat() { nqp::p6list(nqp::list(self), List, Bool::True) }
method hash() { my %h = self }
method hash( :$type = $default, :$of = $default ) {

# your basic hash
if ( $type === $default and $of === $default ) {
my %h = self;
}

# need to add type / of info
else {
my $code= $of === $default ?? "my %h" !! "my {$of.perl} %h";
$code ~= "\{{$type.perl}}" unless $type === $default;
$code ~= " = self";
eval $code;
}
}
method list() { nqp::p6list(nqp::list(self), List, Mu) }
method lol() { MapIter.new(self.list, { .item }, Mu).list }
method pick($n = 1) { self.list.pick($n) }
Expand Down

0 comments on commit 14e7d74

Please sign in to comment.