I'm puzzled about this bit of code where I apparently can't call the WHAT
method in a string concatenation?
my $object = 'Camelia';
say $object;
say $object.WHAT;
say "^name: The object is a " ~ $object.^name;
say "WHAT: The object is a " ~ $object.WHAT;
The output shows that calling ^name
works (a metamethod from Metamodel::ClassHOW), but Perl 6 is confused by .WHAT
as if there's a precedence issue.
Camelia
(Str)
^name: The object is a Str
Use of uninitialized value of type Str in string context
Any of .^name, .perl, .gist, or .say can stringify undefined things, if needed. in block <unit> at meta_methods.p6 line 7
WHAT: The object is a
My Perl 6:
This is Rakudo version 2015.12-219-gd67cb03 built on MoarVM version 2015.12-29-g8079ca5
implementing Perl 6.c.
.WHAT
returns a type object, an undefined objectLike most routines/operators, concatenation assumes its arguments are defined. But the .WHAT
in your last line returns a type object and a type object is not defined. So the result is a warning and stringification to the empty string.
If you want to concatenate an undefined object without generating a warning and instead stringify it into the object's type name you must explicitly .^name
, .gist
or .perl
it eg:
say "The object is a " ~ $object.^name
say "The object is a " ~ $object.WHAT.gist
displays:
The object is a Str
The object is a (Str)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With