T M
class ComplexStr
Error ReportCollection examples

Dual value complex number and string

class ComplexStr 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 $f = <42+0i>say $f.^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 $complex-str = < 42+0i >;
my Complex $complex = $complex-str# OK! 
my Str     $str     = $complex-str# OK! 
say 42+0i  <42+0i  55  1># False; ∈ operator cares about object identity

Methods

method new

method new(Complex $iStr $s)

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

my $f = ComplexStr.new(42+0i, "forty two (but complicated)");
say +$f# OUTPUT: «42+0i␤» 
say ~$f# OUTPUT: «"forty two (but complicated)"␤»

method Bool

Defined as:

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

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.

method Capture

Defined as:

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

Equivalent to Mu.Capture.

method Complex

method Complex

Returns the Complex value of the ComplexStr.

method Numeric

Defined as:

multi method Numeric(ComplexStr:D: --> Complex:D)
multi method Numeric(ComplexStr:U: --> Complex: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+0i>.

method Real

Defined as:

multi method Real(ComplexStr:D: --> Num:D)
multi method Real(ComplexStr:U: --> Num:D)

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.

method Str

Returns the string value of the ComplexStr.

method ACCEPTS

Defined as:

multi method ACCEPTS(ComplexStr: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+0i> ~~ "5.0"# OUTPUT: «False␤» 
say < 5+0i> ~~  5.0 ; # OUTPUT: «True␤» 
say < 5+0i> ~~ <5.0># OUTPUT: «True␤»

Operators

infix cmp

multi sub infix:<cmp>(ComplexStr:D $aComplexStr:D $b)

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 $f = ComplexStr.new(42+0i, "smaller");
my $g = ComplexStr.new(43+0i, "larger");
say $f cmp $g;          # OUTPUT: «Less␤» 
say $f.Str cmp $g.Str;  # OUTPUT: «More␤»