For example, in this class Foo both methods b and c return the same value:
class Foo {
method a { 42 }
method b { $.a }
method c { self.a }
}
my $foo = Foo.new;
say $foo.a; #=> 42
say $foo.b; #=> 42
say $foo.c; #=> 42
I noticed I can do the same thing for the other sigil and twigil combinations, e.g., @.scores vs self.score, %.matches vs self.matches, etc.
Edit: I flagged this question as duplicate, however as librasteve points out I failed to realize the subtle distinction mentioned in the answers for the other question isn't the focal point, and might be easily missed especially by someone new to Raku.
As briefly mentioned in this other SO question's answer,
$.methodis a shorthand for$(self.method)which means the method's return value is itemized or treated in an item or scalar context. Alternatively you can callitemexplicitly onself.method, i.e.,,self.method.item.This contextualization taking place would be more evident if method
areturned a list instead:Methods
aandcreturns the list as-is and methodbhas itemized it. Thus,$.areturns an itemized list whileself.areturns a list without any context.Another way to showcase this is to loop over each method's return value and see how it behaves:
As shown here,
b's return value is treated as a single item, unlike the other methods whereforiterates over each element of the list. Contextualization also happens for@.methodand%.method, as shown here:The following table summarizes the behavior for contextualized method invocations:
$.method$(self.method)self.method.item@.method@(self.method)self.method.list%.method%(self.method)self.method.hash