Dual value rational number and string
is Rat is Str
The dual value types (often referred to as allomorphs) allow for the representation of a value as both a string and a numeric type. Typically they will be created for you when the context is "stringy" but they can be determined to be numbers, such as in some quoting constructs:
my = <42.1>; say .^name; # OUTPUT: «RatStr»
As a subclass of both Rat
and Str
, a RatStr
will be accepted where either is expected. However, RatStr
does not share object identity with Rat
- or Str
-only variants:
my = <42.1>;my Rat = ; # OK!my Str = ; # OK!say 42.1 ∈ <42.1 55 1>; # False; ∈ operator cares about object identity
method new(Rat , Str )
The constructor requires both the Rat
and the Str
value, when constructing one directly the values can be whatever is required:
my = RatStr.new(42.1, "forty two and a bit");say +; # OUTPUT: «42.1»say ~; # OUTPUT: «"forty two and a bit"»
Defined as:
multi method Bool(RatStr: --> Bool)
This method may be provided by the parent classes and not implemented in RatStr directly.
Returns False
if the numerator of the numeric portion is 0
, otherwise returns True
. This applies for < 0/0 >
zero-denominator RatStr as well, despite ?< 0/0 >.Num
being True
. String portion is not considered.
Defined as:
method Capture(RatStr: --> Capture)
Equivalent to Mu.Capture
.
Defined as:
multi method Numeric(RatStr: --> Rat)multi method Numeric(RatStr: --> Rat)
The :D
variant returns the numeric portion of the invocant. The :U
variant issues a warning about using an uninitialized value in numeric context and then returns value 0.0
.
method Rat
Returns the Rat
value of the RatStr
.
Defined as:
multi method Real(Real: --> Rat)multi method Real(Real: --> Rat)
The :D
variant returns the numeric portion of the invocant. The :U
variant issues a warning about using an uninitialized value in numeric context and then returns value 0.0
.
Returns the string value of the RatStr
.
Defined as:
multi method ACCEPTS(RatStr: Any )
If $value
is Numeric (including another allomorph), checks if invocant's Numeric part ACCEPTS the $value
. If $value
is Str, checks if invocant's Str part ACCEPTS the $value
. If value is anything else, checks if both Numeric and Str parts ACCEPTS
the $value
.
say <5.0> ~~ "5"; # OUTPUT: «False»say <5.0> ~~ 5 ; # OUTPUT: «True»say <5.0> ~~ <5>; # OUTPUT: «True»
multi sub infix:<cmp>(RatStr , RatStr )
Compare two RatStr
objects. The comparison is done on the Rat
value first and then on the Str
value. If you want to compare in a different order then you would coerce to the Rat
or Str
values first:
my = RatStr.new(42.1, "smaller");my = RatStr.new(43.1, "larger");say cmp ; # OUTPUT: «Less»say .Str cmp .Str; # OUTPUT: «More»