9

This code works as expected:

sub infix:<mean>(*@a) {
    @a.sum / @a.elems
}
sub Mean (*@a) {
    @a.sum  / @a.elems
}

say EVAL 'Mean 2, 6, 4';     # Output: 4
say EVAL '2 mean 6 mean 4';  # Output: 4

It works as expected when line 7 is in its own scope:

{say EVAL 'Mean 2, 6, 4';}   # Output: 4

But when line 8 is in its own scope:

{say EVAL '2 mean 6 mean 4';} 

===SORRY!=== Error while compiling .../EVAL_1
Two terms in a row
at .../EVAL_1:1
------> 2⏏ mean 6 mean 4
    expecting any of:
        infix
        infix stopper
        statement end
        statement modifier
        statement modifier loop

Why are the two subs treated differently?

1
  • 1
    When I run the code you show in a 2022.02 Rakudo it all works. My two hypotheses are 1: you're using the REPL, which is buggy, (cf Why don't new operator definitions persist in the Raku REPL?) or 2: some related or different bug is in recent Rakudos (which I currently think unlikely but still). I don't have time right now to investigate further. Commented Jul 2, 2023 at 8:39

1 Answer 1

8

This is a known issue, also affecting the REPL.

Problem is that doing a string EVAL will "see" the surrounding scope with all of its additions, so:

say EVAL 'infix:<mean>(infix:<mean>(2, 6), 4)';

will produce 4, because the sub &infix:<mean>(*@a) is known.

But the grammar changes to allow it to work as an infix, are currently not seen inside the EVAL. And that's why you see the compile time error.

I have good hopes we will be able to fix this with the new Raku grammar, based on RakuAST.

Sign up to request clarification or add additional context in comments.

3 Comments

Intriguing! Guess someone needs to do a bisect then.
My summary of a gist summarizing some runs of bisectable is that it broke for most of 2017, and then broke again early this year, and the info bisectable provided was only definitive about what fixed it early in 2018 (a commit by Zoffix).

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.