I've noticed that if I try to define a Function
within a Module
within Compile
, I get errors of (* Compiled expression Function[...] should be a machine-size real number *)
IF those functions are local variables within the Module
AND I don't assign the Module
itself to a variable name within Compile
. Weird, no? I know about using the trailing ..., {{Pattern, _Type}}
syntax, but I don't think that's applicable here (besides, I'm not sure that a type of Function
is listed in the supported types, anyways... -- yet it does appear that they can be used within Compile
).
I'm still too new to Compile
to know whether putting the Function
s within a Module
is even advised, but I read elsewhere that using symbols that aren't shielded within something like a Module
causes calls to MainEvaluate[]
, which considerably slows performance.
Any idea why this happens?
Compile[{},
f1 = Function[{}, 1.0]; (* Fine! *)
f2 = 1.0 &; (* Fine! *)
Module[{f3, f4},
(*f3=Function[{},1.0];*)
(* ABOVE: Compiled expression Function[{}, 1.`] should be a machine-size real number *)
(*f4=1.0&;*) (* Same *)
f5 = Function[{}, 1.0]; (* Fine! *)
f6 = 1.0 &; (* Fine! *)
{f5[], f6[]}
];
mod = Module[{f7, f8},
f7 = Function[{}, 1.0]; (* Fine! *)
f8 = 1.0 &; (* Fine! *)
{f7[], f8[]}
];
mod
][]
```