4

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.

1
  • Upon further inspection, the composition operator doesn't work in the REPL, I think I need to make sure it works in-code. Commented Sep 2, 2024 at 14:13

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

Indeed. Creating an infix operator consists of 2 parts: 1. creating the actual code, 2. adapting the grammar. Sadly, the grammar change does not survive from one statement to the next in the REPL. You can still call the sub though, like infix:<d>(2,10).
strictly speaking, the is a limitation of the Raku REPL implementation rather than a bug ... and a repeat of this SO 64477562

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.