Dual value floating-point number and string
is Num 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.1e0>; say .^name; # OUTPUT: «NumStr»
As a subclass of both Num
and Str
, a NumStr
will be accepted where either is expected. However, NumStr
does not share object identity with Num
- or Str
-only variants:
my = <42e10>;my Num = ; # OK!my Str = ; # OK!say 42e10 ∈ <42e10 55 1>; # False; ∈ operator cares about object identity
method new(Num , Str )
The constructor requires both the Num
and the Str
value, when constructing one directly the values can be whatever is required:
my = NumStr.new(42.1e0, "forty two and a bit");say +; # OUTPUT: «42.1»say ~; # OUTPUT: «"forty two and a bit"»
Defined as:
multi method Bool(NumStr: --> Bool)
This method may be provided by the parent classes and not implemented in NumStr directly.
Returns False
if the invocant is numerically ±0e0
, otherwise returns True
. String portion is not considered.
method Num
Returns the Num
value of the NumStr
.
Defined as:
multi method Numeric(NumStr: --> Num)multi method Numeric(NumStr: --> Num)
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 0e0
.
Defined as:
multi method Real(NumStr: --> Num)multi method Real(NumStr: --> Num)
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 0e0
.
Returns the string value of the NumStr
.
Defined as:
multi method ACCEPTS(NumStr: 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 <5e0> ~~ "5.0"; # OUTPUT: «False»say <5e0> ~~ 5.0 ; # OUTPUT: «True»say <5e0> ~~ <5.0>; # OUTPUT: «True»
multi sub infix:<cmp>(NumStr , NumStr )
Compare two NumStr
objects. The comparison is done on the Num
value first and then on the Str
value. If you want to compare in a different order then you would coerce to a Num
or Str
value first:
my = NumStr.new(42.1e0, "smaller");my = NumStr.new(43.1e0, "larger");say cmp ; # OUTPUT: «Less»say .Str cmp .Str; # OUTPUT: «More»