Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What plus means in method declaration in perl6?

Tags:

raku

What does plus mean in method declarations in Perl6?

Here is an example from spec

submethod BUILD (+$tail, +@legs, *%extraargs) {
    $.tail = $tail;
    @:legs = @legs; 
}
like image 948
teodozjan Avatar asked Apr 11 '13 11:04

teodozjan


2 Answers

2019 Update See the section Variadic positionals destructuring; +@foo and *@foo in my answer to the SO question "variable number of arguments to function/subroutine".


In 2015 Larry Wall introduced the + parameter prefix, one of four parameter prefixes (*, **, +, |) that signify slurpy (variadic) parameters. He added it to the Rakudo compiler, added some tests, gave a brief informal description of it on the irc channel, and added a section on it to the relevant language design doc.


The example quoted in the original question is taken from an archive of an informal document written and frozen in time over a decade ago. At that time a + parameter prefix signified a named parameter as contrasted with a positional one. Nowadays we use : for that, thus:

submethod BUILD (:$tail, :@legs, *%extraargs) {
    $.tail = $tail;
    @.legs = @legs;
}
like image 83
raiph Avatar answered Oct 24 '22 17:10

raiph


Your "spec" links goes to a historical document, and the syntax has long gone from Perl 6. I'm not sure what it used to do, maybe "at least one argument", in analogy to the + quantifier in regexes.

For an up-to-date specification, please read http://perlcabal.org/syn/S06.html which contains all the information on signatures and subroutines.

like image 37
moritz Avatar answered Oct 24 '22 15:10

moritz