Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial implementation of is default(42) on any hash/array

  • Loading branch information...
commit 948210e9e49b5aa2e60642d3ecd0b11bf047dbe8 1 parent b03cc1c
@lizmat lizmat authored
Showing with 105 additions and 33 deletions.
  1. +63 −20 src/core/Array.pm
  2. +42 −13 src/core/Hash.pm
View
83 src/core/Array.pm
@@ -22,22 +22,42 @@ class Array { # declared in BOOTSTRAP
my int $p = nqp::unbox_i($pos.Int);
my Mu $items := nqp::p6listitems(self);
# hotpath check for element existence (RT #111848)
- nqp::existspos($items, $p)
- || nqp::getattr(self, List, '$!nextiter').defined
- && self.exists($p)
- ?? nqp::atpos($items, $p)
- !! nqp::p6bindattrinvres(my $v, Scalar, '$!whence',
- -> { nqp::bindpos($items, $p, $v) } )
+ if nqp::existspos($items, $p)
+ || nqp::getattr(self, List, '$!nextiter').defined
+ && self.exists($p) {
+ nqp::atpos($items, $p);
+ }
+ else {
+ my $default := self.VAR.default;
+ nqp::p6bindattrinvres(
+ my $v = (nqp::istype($default,Any)
+ ?? $default
+ !! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
+ Scalar,
+ '$!whence',
+ -> { nqp::bindpos($items, $p, $v) }
+ );
+ }
}
multi method at_pos(Array:D: int $pos) is rw {
my Mu $items := nqp::p6listitems(self);
# hotpath check for element existence (RT #111848)
- nqp::existspos($items, $pos)
- || nqp::getattr(self, List, '$!nextiter').defined
- && self.exists($pos)
- ?? nqp::atpos($items, $pos)
- !! nqp::p6bindattrinvres(my $v, Scalar, '$!whence',
- -> { nqp::bindpos($items, $pos, $v) } )
+ if nqp::existspos($items, $pos)
+ || nqp::getattr(self, List, '$!nextiter').defined
+ && self.exists($pos) {
+ nqp::atpos($items, $pos);
+ }
+ else {
+ my $default := self.VAR.default;
+ nqp::p6bindattrinvres(
+ my $v = (nqp::istype($default,Any)
+ ?? $default
+ !! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
+ Scalar,
+ '$!whence',
+ -> { nqp::bindpos($items, $pos, $v) }
+ );
+ }
}
proto method bind_pos(|) { * }
@@ -114,16 +134,39 @@ class Array { # declared in BOOTSTRAP
my role TypedArray[::TValue] does Positional[TValue] {
multi method at_pos($pos is copy, TValue $v? is copy) is rw {
$pos = $pos.Int;
- self.exists($pos)
- ?? nqp::atpos(nqp::getattr(self, List, '$!items'), nqp::unbox_i($pos))
- !! nqp::p6bindattrinvres($v, Scalar, '$!whence',
- -> { nqp::bindpos(nqp::getattr(self, List, '$!items'), nqp::unbox_i($pos), $v) } )
+ if self.exists($pos) {
+ nqp::atpos(
+ nqp::getattr(self, List, '$!items'), nqp::unbox_i($pos)
+ );
+ }
+ else {
+ my $default := self.VAR.default;
+ nqp::p6bindattrinvres(
+ $v //= (nqp::istype($default,Any)
+ ?? $default
+ !! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
+ Scalar,
+ '$!whence',
+ -> { nqp::bindpos(
+ nqp::getattr(self,List,'$!items'), nqp::unbox_i($pos),$v) }
+ );
+ }
}
multi method at_pos(int $pos, TValue $v? is copy) is rw {
- self.exists($pos)
- ?? nqp::atpos(nqp::getattr(self, List, '$!items'), $pos)
- !! nqp::p6bindattrinvres($v, Scalar, '$!whence',
- -> { nqp::bindpos(nqp::getattr(self, List, '$!items'), $pos, $v) } )
+ if self.exists($pos) {
+ nqp::atpos(nqp::getattr(self, List, '$!items'), $pos);
+ }
+ else {
+ my $default := self.VAR.default;
+ nqp::p6bindattrinvres(
+ $v //= (nqp::istype($default,Any)
+ ?? $default
+ !! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
+ Scalar,
+ '$!whence',
+ -> { nqp::bindpos(nqp::getattr(self, List,'$!items'),$pos,$v)}
+ );
+ }
}
multi method bind_pos($pos is copy, TValue \bindval) is rw {
$pos = $pos.Int;
View
55 src/core/Hash.pm
@@ -16,10 +16,20 @@ my class Hash { # declared in BOOTSTRAP
nqp::getattr(self, EnumMap, '$!storage') !!
nqp::bindattr(self, EnumMap, '$!storage', nqp::hash());
$key = $key.Str;
- nqp::existskey($storage, nqp::unbox_s($key))
- ?? nqp::atkey($storage, nqp::unbox_s($key))
- !! nqp::p6bindattrinvres(my $v, Scalar, '$!whence',
- -> { nqp::bindkey($storage, nqp::unbox_s($key), $v) } )
+ if nqp::existskey($storage, nqp::unbox_s($key)) {
+ nqp::atkey($storage, nqp::unbox_s($key));
+ }
+ else {
+ my $default := self.VAR.default;
+ nqp::p6bindattrinvres(
+ my $v = (nqp::istype($default,Any)
+ ?? $default
+ !! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any )),
+ Scalar,
+ '$!whence',
+ -> { nqp::bindkey($storage, nqp::unbox_s($key), $v) }
+ );
+ }
}
method bind_key($key, Mu \bindval) is rw {
@@ -199,10 +209,20 @@ my class Hash { # declared in BOOTSTRAP
my role TypedHash[::TValue] does Associative[TValue] {
method at_key(::?CLASS:D: $key is copy, TValue $v? is copy) is rw {
$key = $key.Str;
- self.exists($key)
- ?? nqp::findmethod(EnumMap, 'at_key')(self, $key)
- !! nqp::p6bindattrinvres($v, Scalar, '$!whence',
- -> { nqp::findmethod(EnumMap, 'STORE_AT_KEY')(self, $key, $v) } )
+ if self.exists($key) {
+ nqp::findmethod(EnumMap, 'at_key')(self, $key);
+ }
+ else {
+ my $default := self.VAR.default;
+ nqp::p6bindattrinvres(
+ $v //= (nqp::istype($default,Any)
+ ?? $default
+ !! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
+ Scalar,
+ '$!whence',
+ -> { nqp::findmethod(EnumMap, 'STORE_AT_KEY')(self,$key,$v) }
+ );
+ }
}
method STORE_AT_KEY(Str \key, TValue $x is copy) is rw {
nqp::findmethod(EnumMap, 'STORE_AT_KEY')(self, key, $x);
@@ -228,10 +248,18 @@ my class Hash { # declared in BOOTSTRAP
method keyof () { TKey }
method at_key(::?CLASS:D: TKey \key, TValue $v? is copy) is rw {
my $key_which = key.WHICH;
- self.exists(key)
- ?? nqp::findmethod(EnumMap, 'at_key')(self, $key_which)
- !! nqp::p6bindattrinvres($v, Scalar, '$!whence',
- -> {
+ if self.exists(key) {
+ nqp::findmethod(EnumMap, 'at_key')(self, $key_which);
+ }
+ else {
+ my $default := self.VAR.default;
+ nqp::p6bindattrinvres(
+ $v //= (nqp::istype($default,Any)
+ ?? $default
+ !! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
+ Scalar,
+ '$!whence',
+ -> {
nqp::defined(nqp::getattr(self, $?CLASS, '$!keys')) ||
nqp::bindattr(self, $?CLASS, '$!keys', nqp::hash());
nqp::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
@@ -244,7 +272,8 @@ my class Hash { # declared in BOOTSTRAP
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key_which),
$v);
- })
+ });
+ }
}
method STORE_AT_KEY(TKey \key, TValue $x is copy) is rw {
my $key_which = key.WHICH;
Please sign in to comment.
Something went wrong with that request. Please try again.