Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit ff346adf596d7a812f0d01a3729f4566d03666a3 1 parent ce914fc
Patrick R. Michaud authored March 16, 2013
7  src/core/Exception.pm
@@ -968,6 +968,13 @@ my class X::Str::Trans::InvalidArg is Exception {
968 968
     }
969 969
 }
970 970
 
  971
+my class X::Range::InvalidArg is Exception {
  972
+    has $.got;
  973
+    method message() {
  974
+        "{$.got.^name} objects are not valid endpoints for Ranges";
  975
+    }
  976
+}
  977
+
971 978
 my class X::Sequence::Deduction is Exception {
972 979
     method message() { 'Unable to deduce sequence' }
973 980
 }
31  src/core/Range.pm
... ...
@@ -1,29 +1,36 @@
1  
-class Range is Iterable is Cool does Positional {
  1
+my class X::Range::InvalidArg { ... }
  2
+
  3
+my class Range is Iterable is Cool does Positional {
2 4
     has $.min;
3 5
     has $.max;
4 6
     has $.excludes_min;
5 7
     has $.excludes_max;
6 8
 
7 9
     proto method new(|) { * }
8  
-    multi method new($min, $max, :$excludes_min, :$excludes_max) {
9  
-        nqp::create(self).BUILD($min, $max, $excludes_min, $excludes_max)
  10
+    # The order of "method new" declarations matters here, to ensure
  11
+    # appropriate candidate tiebreaking when mixed type arguments 
  12
+    # are present (e.g., Range,Whatever or Real,Range).
  13
+    multi method new(Range $min, $max, :$excludes_min, :$excludes_max) {
  14
+        X::Range::InvalidArg.new(:got($min)).throw;
10 15
     }
11  
-    multi method new(Real $min, $max, :$excludes_min, :$excludes_max) {
12  
-        nqp::create(self).BUILD($min, $max.Real, $excludes_min, $excludes_max)
  16
+    multi method new($min, Range $max, :$excludes_min, :$excludes_max) {
  17
+        X::Range::InvalidArg.new(:got($max)).throw;
13 18
     }
14  
-    multi method new(Real $min, Whatever $max, :$excludes_min, :$excludes_max) {
15  
-        nqp::create(self).BUILD($min, $Inf, $excludes_min, $excludes_max)
  19
+    multi method new(Whatever $min, Whatever $max, :$excludes_min, :$excludes_max) {
  20
+        fail "*..* is not a valid range";
  21
+    }
  22
+    multi method new(Whatever $min, $max, :$excludes_min, :$excludes_max) {
  23
+        nqp::create(self).BUILD(-$Inf, $max, $excludes_min, $excludes_max)
16 24
     }
17 25
     multi method new($min, Whatever $max, :$excludes_min, :$excludes_max) {
18 26
         nqp::create(self).BUILD($min, $Inf, $excludes_min, $excludes_max)
19 27
     }
20  
-    multi method new(Whatever $min, $max, :$excludes_min, :$excludes_max) {
21  
-        nqp::create(self).BUILD(-$Inf, $max, $excludes_min, $excludes_max)
  28
+    multi method new(Real $min, $max, :$excludes_min, :$excludes_max) {
  29
+        nqp::create(self).BUILD($min, $max.Real, $excludes_min, $excludes_max)
22 30
     }
23  
-    multi method new(Whatever $min, Whatever $max, :$excludes_min, :$excludes_max) {
24  
-        fail "*..* is not a valid range";
  31
+    multi method new($min, $max, :$excludes_min, :$excludes_max) {
  32
+        nqp::create(self).BUILD($min, $max, $excludes_min, $excludes_max)
25 33
     }
26  
-    
27 34
 
28 35
     submethod BUILD($min, $max, $excludes_min, $excludes_max) {
29 36
         $!min = $min;

0 notes on commit ff346ad

Please sign in to comment.
Something went wrong with that request. Please try again.