Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 4
    $\begingroup$ Compile[{{x, _Real}}, Evaluate[x^2 + g[x]]] $\endgroup$ Commented Mar 13, 2020 at 17:30
  • 1
    $\begingroup$ I get no error in V12 from g[x_] := x^3; cf = Compile[{{x, _Real}}, x^2 + g[x]], although it and @ilian's suggestion both contain MainEvaluate[]. Note the argument to g is a pattern x_, not a symbol x. $\endgroup$ Commented Mar 13, 2020 at 21:45
  • $\begingroup$ @ilian I wouldn't do that, without at least wrapping a Block[{x}, ...] around Compile. But even then, I think it is generally an error - prone practice, unless you are in full control of exact evaluation path for the stuff inside Evaluate. $\endgroup$ Commented Mar 13, 2020 at 23:09
  • $\begingroup$ Fair enough, I'm happy to retract my attempt at commenting $\endgroup$ Commented Mar 13, 2020 at 23:23
  • 2
    $\begingroup$ A bit cumbersome but very robust is this way: Block[{x}, With[{code = x^2 + g[x]}, Compile[{{x, _Real}}, code]]]. $\endgroup$ Commented Mar 14, 2020 at 7:44