Pergunta

What does plus mean in method declarations in Perl6?

Here is an example from spec

submethod BUILD (+$tail, +@legs, *%extraargs) {
    $.tail = $tail;
    @:legs = @legs; 
}
Foi útil?

Solução

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;
}

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top