Skip to content

Commit

Permalink
Support binding of hash elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Jan 13, 2012
1 parent 919912a commit 2ea20a2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/core/Any.pm
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,25 @@ my class Any {
########
proto method postcircumfix:<{ }>(|$) { * }
multi method postcircumfix:<{ }>() { self.values }
multi method postcircumfix:<{ }>(:$BIND!) { die "Cannot bind to a zen hash slice" }
multi method postcircumfix:<{ }>($key) is rw {
self.at_key($key)
}
multi method postcircumfix:<{ }>($key, :$BIND! is parcel) is rw {
self.bind_key($key, $BIND)
}
multi method postcircumfix:<{ }>(Positional $key) is rw {
$key.map({ self{$_} }).eager.Parcel
}
multi method postcircumfix:<{ }>(Positional $key, :$!BIND) is rw {
die "Cannot bind to a hash slice"
}
multi method postcircumfix:<{ }>(Whatever) is rw {
self{self.keys}
}
multi method postcircumfix:<{ }>(Whatever, :$BIND!) is rw {
die "Cannot bind to a whatever hash slice"
}

method reduce(&with) { self.list.reduce(&with) }
}
Expand Down
17 changes: 17 additions & 0 deletions src/core/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ my class Hash {
-> { pir::find_method__PPs(EnumMap, 'STORE_AT_KEY')(self, $key, $v) } )
}

method bind_key($key, \$bindval) is rw {
pir::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
nqp::bindkey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key.Str),
$bindval)
}

multi method perl(Hash:D \$self:) {
nqp::iscont($self)
?? '{' ~ self.pairs.map({.perl}).join(', ') ~ '}'
Expand Down Expand Up @@ -99,6 +108,14 @@ my class Hash {
method STORE_AT_KEY(Str \$key, TValue $x is copy) is rw {
pir::find_method__PPs(EnumMap, 'STORE_AT_KEY')(self, $key, $x);
}
method bind_key($key, TValue \$bindval) is rw {
pir::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
nqp::bindkey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key.Str),
$bindval)
}
}
method PARAMETERIZE_TYPE(Mu $t) {
self but TypedHash[$t.WHAT]
Expand Down

0 comments on commit 2ea20a2

Please sign in to comment.