10

In Raku functions documentation, there is an example as follows:

enum DebugType <LOG WARNING ERROR>;
 
#|[ Prints a message to stderr with a color-coded key. ] 
proto debug(DebugType:D $type, Str:D $message --> Bool:_) {
    note sprintf qb/\e[1;%dm[%s]\e[0m %s/, {*}, $type.key, $message
}
multi debug(LOG;; Str:D --> 32)     { }
multi debug(WARNING;; Str:D --> 33) { }
multi debug(ERROR;; Str:D --> 31)   { }

I am trying to figurue out the usage of double semicolons after LOG, WARNING, and ERROR, and can only find it in Raku Signatures documentation. Under subtitle 'Long names', it's said that:

"To exclude certain parameters from being considered in multiple dispatch, separate them with a double semicolon." And its example code is:

multi sub f(Int $i, Str $s;; :$b) { say "$i, $s, {$b.raku}" };
f(10, 'answer');
# OUTPUT: «10, answer, Any␤»

But in the above multi debug subs, why do we have to exclude their first argument from being considered in multiple dispatch? Once they are excluded, how could Raku know which debug sub to dispatch? I tried to replace each double semicolon with a 'normal' single comma, but could not find anything different in the output. Besides, the f sub example makes me puzzled. There's only one multi sub there. How double semicolon affect its dispatch? The subtitle 'Long names' is weird, too. Thanks a lot in advance for any kind explanation.

1 Answer 1

15

The purpose of the ;; parameter separator is to indicate that the parameters after it do not participate in the multiple dispatch process. Thus in the debug example, only the log level value is considered when deciding what to dispatch to.

The term "long name" is expressing the idea that the identity of the multi candidate is not just the name of the subroutine, such as 'debug', but also involves the types of its parameters (or those up to the ;;). It's one way to look at it - in fact, in a language like C++ a long name really is generated as part of the compilation process - although in Raku's case the multi candidates are held in a list attached to the controlling proto, rather than being name-mangled and put into a symbol table.

Use of ;; is relatively rare in practice. I'd say its best use is in communicating to the reader of the program which parameters are used to select the dispatch candidate and which aren't.

There's not really a performance reason to use ;;, or at least not on MoarVM. While it might look like it would cause less work at runtime, in reality the JIT specializes hot-path subs/methods by their input types, and that would consider arguments after the ;; in checking that the specialization can be used.

2
  • 2
    Jonathan, thank you very much for your detailed clarification. I am sorry for my poor English comprehension. But I think the document writer should have more clearly pointed out that all the parameters after the double semicolon do not participate in the multiple dispatch process, just like what you said.
    – AlvaPan
    Mar 12 at 16:23
  • 2
    alvaplan, I was going to suggest that you might like to raise this as an issue at raku docs github issues, then I noticed that this has been opened by a helpful elf already: github.com/Raku/doc/issues/4268 ... please do feel free to propose better words on that thread and to feedback your response...
    – librasteve
    Mar 13 at 12:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.