Skip to content

Commit

Permalink
Define Bridge method for the real types.
Browse files Browse the repository at this point in the history
This is a cast type to a lingua franca bridge type for real numbers.  (Was called RealX in my blog posts on the subject.  Am open to better suggestions than Bridge, but wanted to get the concept working.)
  • Loading branch information
colomon committed Apr 4, 2010
1 parent 32fda13 commit 6fd5479
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/Int.pm
Expand Up @@ -22,6 +22,10 @@ augment class Int does Real {
self.Num.ACCEPTS($other);
}

method Bridge() {
self.Num;
}

our Bool multi method Bool() { self != 0 ?? Bool::True !! Bool::False }

our Int multi method Int() { self }
Expand Down
5 changes: 5 additions & 0 deletions src/core/Num.pm
Expand Up @@ -8,6 +8,7 @@ augment class Num does Real {
$other == self;
}
}

multi method ACCEPTS(Complex $other) {
if self eq 'NaN' {
$other.re eq 'NaN' || $other.im eq 'NaN';
Expand All @@ -16,6 +17,10 @@ augment class Num does Real {
}
}

method Bridge() {
self;
}

multi method Bool() {
self != 0.0e0
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/Rat.pm
Expand Up @@ -34,6 +34,11 @@ class Rat does Real {

multi method perl() { "$!numerator/$!denominator"; }

method Bridge() {
$!denominator == 0 ?? Inf * $!numerator.sign
!! $!numerator.Bridge / $!denominator.Bridge;
}

our Bool multi method Bool() { $!numerator != 0 ?? Bool::True !! Bool::False }

multi method Num() {
Expand Down
4 changes: 4 additions & 0 deletions src/core/Real.pm
@@ -1,4 +1,8 @@
role Real does Numeric {
method Bridge() {
fail "Bridge must be defined for the Real type " ~ self.WHAT;
}

method abs() {
self < 0 ?? -self !! self;
}
Expand Down

0 comments on commit 6fd5479

Please sign in to comment.