Dual value number and string
is Str
The Allomorph
class is a common parent class for Raku's dual value types: ComplexStr
, IntStr
, NumStr
, RatStr
.
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+0i>; say .^name; # OUTPUT: «ComplexStr»my = <42>; say .^name; # OUTPUT: «IntStr»my = <42.1e0>; say .^name; # OUTPUT: «NumStr»my = <42.1>; say .^name; # OUTPUT: «RatStr»
As a subclass of both a Numeric
class and Str
, via the Allomorph
class, an allomorph will be accepted where either is expected. However, an allomorph does not share object identity with its Numeric
parent class- or Str
-only variants:
my (, , , )= < 42+0i 42 42e10 42.1 >;my (Complex , Int , Num , Rat )= , , , ; # OK!my Str= , , , ; # OK!# ∈ operator cares about object identitysay 42+0i ∈ < 42+0i 42 42e10 42.1 >; # OUTPUT: «False»say 42 ∈ < 42+0i 42 42e10 42.1 >; # OUTPUT: «False»say 42e10 ∈ < 42+0i 42 42e10 42.1 >; # OUTPUT: «False»say 42.1 ∈ < 42+0i 42 42e10 42.1 >; # OUTPUT: «False»
Please see the Numerics page for a more complete description on how to work with these allomorphs.
multi method ACCEPTS(Allomorph: Any \a)
If the a
parameter is Numeric
(including another allomorph), checks if invocant's Numeric
value ACCEPTS a
. If the a
parameter is Str
, checks if invocant's Str
value ACCEPTS a
. If the a
parameter is anything else, checks if both Numeric
and Str
values of the invocant ACCEPTS
a
.
say "5.0" ~~ <5>; # OUTPUT: «False»say 5.0 ~~ <5>; # OUTPUT: «True»say <5.0> ~~ <5>; # OUTPUT: «True»
multi method Bool(::?CLASS:)
Returns False
if the invocant is numerically 0
, otherwise returns True
. The Str
value of the invocant is not considered.
Note: For the Allomorph
subclass RatStr
also see Rational.Bool
.
method chomp(Allomorph:)
Calls Str.chomp
on the invocant's Str
value.
method chop(Allomorph: |c)
Calls Str.chop
on the invocant's Str
value.
method comb(Allomorph: |c)
Calls Str.comb
on the invocant's Str
value.
method fc(Allomorph:)
Calls Str.fc
on the invocant's Str
value.
method flip(Allomorph:)
Calls Str.flip
on the invocant's Str
value.
method lc(Allomorph:)
Calls Str.lc
on the invocant's Str
value.
method pred(Allomorph:)
Calls Numeric.pred
on the invocant's numeric value.
multi method raku(Allomorph:)
Return a representation of the object that can be used via EVAL
to reconstruct the value of the object.
method samecase(Allomorph: |c)
Calls Str.samecase
on the invocant's Str
value.
method samemark(Allomorph: |c)
Calls Str.samemark
on the invocant's Str
value.
method split(Allomorph: |c)
Calls Str.split
on the invocant's Str
value.
method Str(Allomorph:)
Returns the Str
value of the invocant.
method subst(Allomorph: |c)
Calls Str.subst
on the invocant's Str
value.
method subst-mutate(Allomorph \SELF: |c)
Calls Str.subst-mutate
on the invocant's Str
value.
method substr(Allomorph: |c)
Calls Str.substr
on the invocant's Str
value.
method substr-rw(Allomorph \SELF: = 0, = Whatever)
Calls Str.substr-rw
on the invocant's Str
value.
method succ(Allomorph:)
Calls Numeric.succ
on the invocant's numeric value.
method tc(Allomorph:)
Calls Str.tc
on the invocant's Str
value.
method tclc(Allomorph:)
Calls Str.tclc
on the invocant's Str
value.
method trim(Allomorph:)
Calls Str.trim
on the invocant's Str
value.
method trim-leading(Allomorph:)
Calls Str.trim-leading
on the invocant's Str
value.
method trim-trailing(Allomorph:)
Calls Str.trim-trailing
on the invocant's Str
value.
method uc(Allomorph:)
Calls Str.uc
on the invocant's Str
value.
multi method WHICH(Allomorph:)
Returns an object of type ValueObjAt
which uniquely identifies the object.
my = <42.1e0>;say .WHICH; # OUTPUT: «NumStr|Num|42.1|Str|42.1e0»
multi sub infix:<cmp>(Allomorph , Allomorph )
Compare two Allomorph
objects. The comparison is done on the Numeric
value first and then on the Str
value. If you want to compare in a different order then you would coerce to an Numeric
or Str
value first:
my = IntStr.new(42, "smaller");my = IntStr.new(43, "larger");say cmp ; # OUTPUT: «Less»say .Str cmp .Str; # OUTPUT: «More»
multi sub infix:<eqv>(Allomorph , Allomorph --> Bool)
Returns True
if the two Allomorph
$a
and $b
are of the same type, their Numeric
values are equivalent and their Str
values are also equivalent. Returns False
otherwise.