syntaxrakudice

Defining bareword operators in Raku


I have some questions regarding defining bareword operators in Raku. As it stands in the language, there are a variety of bareword operators, such as div, mod, gcd, & lcm. As I'm curious about the capabilities of this langauge, I am attempting to implement Dice Notation via the infix:<d> syntax like so:

 sub infix:<d>(Int $x, Int $y) {
        return (1..$y).roll($x)
}

But whenever I attempt to use something like 2 d 10, the compiler interprets it as a term, rather than an operator.

Is it even possible to define bareword operators in the first place, and if so, how can I do such?

I would also like to note that I have only tested the above code in the REPL.


Solution

  • Upon further inspection, this appears to be a bug in the REPL.

    Bareword operators can be defined in a script, but not used in it's provided REPL, for example

    sub infix:< d >(Int $x, Int $y) {
        return (1..$y).roll($x)
    }
    sub prefix:<d >(Int $x) {
        return $x.rand.Int+1
    }
    
    say 2 d 10;
    say d 20
    

    produces

    (4 5)
    9
    

    when ran as a script, but when entered into the REPL as a single line statement...

    Two terms in a row
    ------> say 2⏏ d 10
        expecting any of:
            infix
            infix stopper
            postfix
            statement end
            statement modifier
            statement modifier loop