Dual value complex number and string
is Complex 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+0i>; say .^name; # OUTPUT: «ComplexStr»
As a subclass of both Complex
and Str
, a ComplexStr
will be accepted where either is expected. However, ComplexStr
does not share object identity with Complex
- or Str
-only variants:
my = < 42+0i >;my Complex = ; # OK!my Str = ; # OK!say 42+0i ∈ <42+0i 55 1>; # False; ∈ operator cares about object identity
method new(Complex , Str )
The constructor requires both the Complex
and the Str
value, when constructing one directly the values can be whatever is required:
my = ComplexStr.new(42+0i, "forty two (but complicated)");say +; # OUTPUT: «42+0i»say ~; # OUTPUT: «"forty two (but complicated)"»
Defined as:
multi method Bool(ComplexStr: --> Bool)
This method may be provided by the parent classes and not implemented in ComplexStr directly.
Returns False
if the invocant is numerically ±0±0i
, otherwise returns True
. String portion is not considered.
Defined as:
method Capture(ComplexStr: --> Capture)
Equivalent to Mu.Capture
.
method Complex
Returns the Complex
value of the ComplexStr
.
Defined as:
multi method Numeric(ComplexStr: --> Complex)multi method Numeric(ComplexStr: --> Complex)
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+0i>
.
Defined as:
multi method Real(ComplexStr: --> Num)multi method Real(ComplexStr: --> Num)
Coerces the numeric portion of the invocant to Num. If the imaginary part isn't approximately zero, coercion fails with X::Numeric::Real
.
The :D
variant returns the result of that coercion. 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 ComplexStr
.
Defined as:
multi method ACCEPTS(ComplexStr: 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+0i> ~~ "5.0"; # OUTPUT: «False»say < 5+0i> ~~ 5.0 ; # OUTPUT: «True»say < 5+0i> ~~ <5.0>; # OUTPUT: «True»
multi sub infix:<cmp>(ComplexStr , ComplexStr )
Compare two ComplexStr
objects. The comparison is done on the Complex
value first and then on the Str
value. If you want to compare in a different order then you would coerce to the Complex
or Str
values first:
my = ComplexStr.new(42+0i, "smaller");my = ComplexStr.new(43+0i, "larger");say cmp ; # OUTPUT: «Less»say .Str cmp .Str; # OUTPUT: «More»