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?
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.