Documentation for method Str.val

From Str

(Str) sub val

multi sub val(Str:D $MAYBEVAL, :$val-or-fail)

Given a Str that may be parsable as a numeric value, it will attempt to construct the appropriate allomorph, returning one of IntStr, NumStr, RatStr or ComplexStr or a plain Str if a numeric value cannot be parsed. If the :val-or-fail adverb is provided it will return an X::Str::Numeric rather than the original string if it cannot parse the string as a number.

say val("42").WHAT;    # OUTPUT: «(IntStr)␤»
say val("42e0").WHAT;  # OUTPUT: «(NumStr)␤»
say val("42.0").WHAT;  # OUTPUT: «(RatStr)␤»
say val("42+0i").WHAT; # OUTPUT: «(ComplexStr)␤»