Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Perl 6's string concatenation not like .WHAT?

Tags:

raku

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.
like image 241
brian d foy Avatar asked Mar 05 '16 19:03

brian d foy


1 Answers

.WHAT returns a type object, an undefined object

Like 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)
like image 190
raiph Avatar answered Oct 14 '22 06:10

raiph