Skip to content

Instantly share code, notes, and snippets.

@b2gills
Created January 29, 2015 21:16
Show Gist options
  • Save b2gills/8f942741303f7758c6ed to your computer and use it in GitHub Desktop.
Save b2gills/8f942741303f7758c6ed to your computer and use it in GitHub Desktop.
IntStr et all test
NumStr
gist: 5e0
Str: 5e0 Str
Numeric: 5 Num
Bridge: 5 Num
Numeric True
Int False
Rat False
Num True
Str False
converts: True
Num
RatStr
gist: 10/2
Str: 10/2 Str
Numeric: 5 Rat
Bridge: 5 Rat
Numeric True
Int False
Rat True
Num False
Str False
converts: True
Rat
IntStr
gist: 5
Str: 5 Str
Numeric: 5 Int
Bridge: 5 Int
Numeric True
Int True
Rat False
Num False
Str False
converts: True
Int
RatStr
gist: 355/113
Str: 355/113 Str
Numeric: 3.141593 Rat
Bridge: 3.141593 Rat
Numeric True
Int False
Rat True
Num False
Str False
converts: True
Rat
(IntStr) (Val) (Stringy) (Int) (Cool) (Any) (Mu)
(NumStr) (Val) (Stringy) (Num) (Cool) (Any) (Mu)
(RatStr) (Val) (Stringy) (Rat) (Cool) (Any) (Mu)
#! /usr/bin/env perl6
class NumStr {...}
class IntStr {...}
class RatStr {...}
# can't replace Stringy for Str
# or there will be an error when type() is called
class Val is Stringy does Numeric {
has Str $!Str;
multi method new (Val:U: Str $Str){
given $Str.Numeric {
when Int { IntStr.new( :$Str, :Numeric($_) ) }
when Num { NumStr.new( :$Str, :Numeric($_) ) }
when Rat { RatStr.new( :$Str, :Numeric($_) ) }
}
}
submethod BUILD (:$!Str){ }
multi method Str (Val:D:){$!Str}
multi method Numeric (Val:D:){ ... }
multi method Bridge (Val:D:){ $.Numeric }
multi method Real (Val:D:){ $.Numeric.Real }
multi method Rat (Val:D:){ $.Numeric.Rat }
multi method Num (Val:D:){ $.Numeric.Num }
multi method Int (Val:D:){ $.Numeric.Int }
multi method gist (Val:D:){ $.Str }
# copied from Str
multi method WHICH(Val:D:) {
nqp::box_s(
nqp::concat(
nqp::concat(nqp::unbox_s(self.^name), '|'),
$!Str
),
ObjAt
);
}
}
class IntStr is Val is Int {
has Int $!Numeric;
multi method Numeric (IntStr:D:){$!Numeric}
submethod BUILD ( :$!Numeric ){}
}
class RatStr is Val is Rat {
has Rat $!Numeric;
multi method Numeric (RatStr:D:){$!Numeric}
submethod BUILD ( :$!Numeric ){}
}
class NumStr is Val is Num {
has Num $!Numeric;
multi method Numeric (NumStr:D:){$!Numeric}
submethod BUILD ( :$!Numeric ){}
}
sub val(Str $s) returns Val {
return Val.new($s);
}
sub test-type($a, ::T $){
say T.^name, "\t", $a ~~ T;
}
multi type(Num $){Num}
multi type(Int $){Int}
multi type(Rat $){Rat}
multi type(Str $){Str}
for <5e0 10/2 5 355/113> -> $s {
my $v = val($s);
say $v.^name;
say 'gist: ', $v;
say 'Str: ',$v.Str, ' ', $v.Str.^name;
say 'Numeric: ', $v.Numeric,' ', $v.Numeric.^name;
say 'Bridge: ', $v.Bridge,' ', $v.Bridge.^name;
''.say;
test-type($v,$_) for Numeric, Int, Rat, Num, Str;
say 'converts: ', so all $v.Rat ~~ Rat, $v.Int ~~ Int, $v.Num ~~ Num;
type($v).^name.say;
''.say;
}
IntStr.^mro.say;
NumStr.^mro.say;
RatStr.^mro.say;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment