Skip to content

Commit

Permalink
Merge pull request #4687 from vrurg/rakudo_1809-numeric-ranges
Browse files Browse the repository at this point in the history
Make ranges coerce LHS into a numeric if range boundaries are numeric
  • Loading branch information
vrurg committed Dec 27, 2021
2 parents e190245 + 7f73dcb commit 0ab6df2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
21 changes: 17 additions & 4 deletions src/core.c/Range.pm6
Expand Up @@ -406,10 +406,23 @@ my class Range is Cool does Iterable does Positional {
and (topic cmp $!max) < +(!$!excludes-max)
}
multi method ACCEPTS(Range:D: Cool:D \got) {
$!is-int && nqp::istype(got,Int)
?? got >= $!min + $!excludes-min && got <= $!max - $!excludes-max
!! ($!excludes-min ?? got after $!min !! not got before $!min)
&& ($!excludes-max ?? got before $!max !! not got after $!max)
nqp::if(
$!is-int && nqp::istype(got, Int),
nqp::if(got >= $!min + $!excludes-min,
got <= $!max - $!excludes-max),
nqp::if(
(nqp::istype($!min, Numeric) && nqp::istype($!max, Numeric)),
nqp::stmts(
(my \got-num = nqp::if(nqp::istype(got, Numeric), got, got.Numeric(:fail-or-nil))),
nqp::if(
nqp::istype(got-num, Nil),
False,
nqp::if(
nqp::if($!excludes-min, got-num > $!min, got-num >= $!min),
nqp::if($!excludes-max, got-num < $!max, got-num <= $!max)))),
nqp::if(
nqp::if($!excludes-min, got after $!min, not got before $!min),
nqp::if($!excludes-max, got before $!max, not got after $!max))))
}
multi method ACCEPTS(Range:D: Complex:D \got) {
nqp::istype(($_ := got.Real), Failure) ?? False !! nextwith $_
Expand Down
4 changes: 2 additions & 2 deletions src/core.c/Str.pm6
Expand Up @@ -794,7 +794,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
)
)
}
multi method Numeric(Str:D: Bool :$fail-or-mu --> Numeric:D) {
multi method Numeric(Str:D: Bool :$fail-or-nil --> Numeric:D) {
#?if !jvm
# check for any combining characters
nqp::isne_i(nqp::chars(self),nqp::codes(self))
Expand Down Expand Up @@ -822,7 +822,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
self,0,nqp::chars(self)
) == nqp::chars(self)
?? 0 # just spaces
!! val(self, :val-or-fail, :$fail-or-mu) # take the slow route
!! val(self, :val-or-fail, :$fail-or-nil) # take the slow route
}

multi method gist(Str:D:) { self }
Expand Down
6 changes: 3 additions & 3 deletions src/core.c/allomorphs.pm6
Expand Up @@ -264,7 +264,7 @@ multi sub val(\one-thing) is raw {
one-thing
}

multi sub val(Str:D $MAYBEVAL, Bool :$val-or-fail, Bool :$fail-or-mu) {
multi sub val(Str:D $MAYBEVAL, Bool :$val-or-fail, Bool :$fail-or-nil) {
# TODO:
# * Additional numeric styles:
# + fractions in [] radix notation: :100[10,'.',53]
Expand All @@ -288,8 +288,8 @@ multi sub val(Str:D $MAYBEVAL, Bool :$val-or-fail, Bool :$fail-or-mu) {
# string, or a failure if we're Str.Numeric
my &parse_fail := -> \msg {
$val-or-fail
?? $fail-or-mu
?? return Mu
?? $fail-or-nil
?? return Nil
!! fail X::Str::Numeric.new(:source($MAYBEVAL),:reason(msg),:$pos)
!! return $MAYBEVAL
}
Expand Down

0 comments on commit 0ab6df2

Please sign in to comment.