Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Throw exception when Range endpoint is used to create a Range.
  • Loading branch information
pmichaud committed Mar 16, 2013
1 parent ce914fc commit ff346ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/core/Exception.pm
Expand Up @@ -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' }
}
Expand Down
31 changes: 19 additions & 12 deletions src/core/Range.pm
@@ -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;
Expand Down

0 comments on commit ff346ad

Please sign in to comment.