I think I answered my own question via CompilePrint
(just learned about this); will post for posterity:
ClearAll(f1, f2, f3, f4, f5, f6, f7, f8)
f3 := Function(y, y^4);
c1 = Compile({x},
m1 = Module({f1},
f1 = Function(y, y^2);
f1(x)
);
Module({},
{Function(y, y^3)(x), f3(x)}
)
);
c1(2)
m1
Needs("CompiledFunctionTools`")
CompilePrint(c1)
1 V17 = MainEvaluate(
2
Function({x}, m1 = Module({f1}, f1 = Function(y, y ); f1(x)))( R0))
2 R2 = R0
3 R1 = Power( R2, I0)
4 R2 = MainEvaluate( Hold(f3)( R0))
5 T(R1)0 = {R1, R2}
6 Return
It looks like the versions that I thought were “working” were just running in non-compiled mode (not just the assignments, either — the whole step). I’m used to getting an error/warning in such cases, but I had to look at CompilePrint
to see it this time.
I guess you just have to use anonymous functions exclusively. It looks like BOTH the methods in my original post AND defining the Function
outside of Compile
altogether (as I indicated I was unsure-about in my original post) cause MainEvaluate()
to be used…
I could’ve sworn I’d come across other examples on StackOverflow where a function was defined outside of Compile
and then used within it, but I’ll have to educate myself further.