T M
class RatStr
Error ReportCollection examples

Dual value rational number and string

class RatStr 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 $f = <42.1>say $f.^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 $rat-str = <42.1>;
my Rat $rat = $rat-str# OK! 
my Str $str = $rat-str# OK! 
say 42.1  <42.1  55  1># False; ∈ operator cares about object identity

Methods

method new

method new(Rat $iStr $s)

The constructor requires both the Rat and the Str value, when constructing one directly the values can be whatever is required:

my $f = RatStr.new(42.1"forty two and a bit");
say +$f# OUTPUT: «42.1␤» 
say ~$f# OUTPUT: «"forty two and a bit"␤»

method Bool

Defined as:

multi method Bool(RatStr:D: --> Bool:D)

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.

method Capture

Defined as:

method Capture(RatStr:D: --> Capture:D)

Equivalent to Mu.Capture.

method Numeric

Defined as:

multi method Numeric(RatStr:D: --> Rat:D)
multi method Numeric(RatStr:U: --> Rat:D)

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

method Rat

Returns the Rat value of the RatStr.

method Real

Defined as:

multi method Real(Real:D: --> Rat:D)
multi method Real(Real:U: --> Rat:D)

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 Str

Returns the string value of the RatStr.

method ACCEPTS

Defined as:

multi method ACCEPTS(RatStr:D: Any:D $value)

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␤»

Operators

infix cmp

multi sub infix:<cmp>(RatStr:D $aRatStr:D $b)

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 $f = RatStr.new(42.1"smaller");
my $g = RatStr.new(43.1"larger");
say $f cmp $g;          # OUTPUT: «Less␤» 
say $f.Str cmp $g.Str;  # OUTPUT: «More␤»