Apply OwnValues of argument after DownValues of function

While I agree with the opinion that the need for serious changes in evaluation sequence in most cases means that the approach isn't best, here is one possible way of achieving what you ask - use conditional OwnValue:

f[e] = 1;
g[e] = 2;
e /; ! MemberQ[Stack[_], HoldForm[(f | g)[e]]] := 3;

Now

e

(* 3 *)

f[e]

(* 1 *)

g[e]

(* 2 *)

This solution has a number of issues however, it is rather fragile (uses entire stack, so is non-local) and also performance can be bad. So I would still try to reconsider the approach.


I am not sure this might be useful, but if I want to evalutate f[e] before e, then I simply would not evaluate e

f[e] = 1;
g[e] = 2;
e = 3;

f[e//Unevaluated]

1

g[e//Unevaluated]

2


Alternatively:

ClearAll[f, e]
f[e] = {1, e};
e = 3;

f[e]

f[3]

Block[{e}, f[e]]

{1, 3}

And if there are other types of values associated with e like:

ClearAll[e, f];
e /: f[e] := {100, e};
e = 5;

Then:

Internal`InheritedBlock[ {e},
 e =.;
 f[e]
]

{100, 5}

Tags:

Evaluation