Permalink
Browse files

Throw exception when Range endpoint is used to create a Range.

  • Loading branch information...
1 parent ce914fc commit ff346adf596d7a812f0d01a3729f4566d03666a3 @pmichaud pmichaud committed Mar 16, 2013
Showing with 26 additions and 12 deletions.
  1. +7 −0 src/core/Exception.pm
  2. +19 −12 src/core/Range.pm
@@ -968,6 +968,13 @@ my class X::Str::Trans::InvalidArg is Exception {
}
}
+my class X::Range::InvalidArg is Exception {
+ has $.got;
+ method message() {
+ "{$.got.^name} objects are not valid endpoints for Ranges";
+ }
+}
+
my class X::Sequence::Deduction is Exception {
method message() { 'Unable to deduce sequence' }
}
View
@@ -1,29 +1,36 @@
-class Range is Iterable is Cool does Positional {
+my class X::Range::InvalidArg { ... }
+
+my class Range is Iterable is Cool does Positional {
has $.min;
has $.max;
has $.excludes_min;
has $.excludes_max;
proto method new(|) { * }
- multi method new($min, $max, :$excludes_min, :$excludes_max) {
- nqp::create(self).BUILD($min, $max, $excludes_min, $excludes_max)
+ # The order of "method new" declarations matters here, to ensure
+ # appropriate candidate tiebreaking when mixed type arguments
+ # are present (e.g., Range,Whatever or Real,Range).
+ multi method new(Range $min, $max, :$excludes_min, :$excludes_max) {
+ X::Range::InvalidArg.new(:got($min)).throw;
}
- multi method new(Real $min, $max, :$excludes_min, :$excludes_max) {
- nqp::create(self).BUILD($min, $max.Real, $excludes_min, $excludes_max)
+ multi method new($min, Range $max, :$excludes_min, :$excludes_max) {
+ X::Range::InvalidArg.new(:got($max)).throw;
}
- multi method new(Real $min, Whatever $max, :$excludes_min, :$excludes_max) {
- nqp::create(self).BUILD($min, $Inf, $excludes_min, $excludes_max)
+ multi method new(Whatever $min, Whatever $max, :$excludes_min, :$excludes_max) {
+ fail "*..* is not a valid range";
+ }
+ multi method new(Whatever $min, $max, :$excludes_min, :$excludes_max) {
+ nqp::create(self).BUILD(-$Inf, $max, $excludes_min, $excludes_max)
}
multi method new($min, Whatever $max, :$excludes_min, :$excludes_max) {
nqp::create(self).BUILD($min, $Inf, $excludes_min, $excludes_max)
}
- multi method new(Whatever $min, $max, :$excludes_min, :$excludes_max) {
- nqp::create(self).BUILD(-$Inf, $max, $excludes_min, $excludes_max)
+ multi method new(Real $min, $max, :$excludes_min, :$excludes_max) {
+ nqp::create(self).BUILD($min, $max.Real, $excludes_min, $excludes_max)
}
- multi method new(Whatever $min, Whatever $max, :$excludes_min, :$excludes_max) {
- fail "*..* is not a valid range";
+ multi method new($min, $max, :$excludes_min, :$excludes_max) {
+ nqp::create(self).BUILD($min, $max, $excludes_min, $excludes_max)
}
-
submethod BUILD($min, $max, $excludes_min, $excludes_max) {
$!min = $min;

0 comments on commit ff346ad

Please sign in to comment.