You can make use of Language`ExtendedDefinition
which is used to move code from a desktop to the cloud, so should be robust (in this example, it is also possible to use DownValues
in a similar fashion). Basically, get the extended definition of your function g
at the time of interest, and then use that function in the definition of the function f
. Here is an example of a function g
:
g[1] = "one";
g[n_Integer] = "integer";
g[n_Real] = "real";
g[n_Rational] = "rational";
Using Language`ExtendedDefinition
to burn this definition of g
into the definition of f
:
With[{def = Language`ExtendedDefinition["g"]},
f[x_] := Block[{g},
Language`ExtendedDefinition["g"] = def;
If[x<3, g[x], 0]
]
]
Now, clear g
:
Clear[g]
Check whether f
still behaves as expected:
f[1]
f[2]
f[3/2]
f[1.5]
f[10]
"one"
"integer"
"rational"
"real"
0