What result would you expect from the brief dust-up below?
x = 5;
D[x^2, x]
(* During evaluation of In[89]:= General::ivar: 5 is not a valid variable. *)
(* Out[90]= D[25, 5] *)
No magic of localizing variables is going to salvage this. It's user error, or is some cases what the user intended, but regardless it is definitely as designed.
There are gray areas to all this. For example, antiderivatives (aka indefinite integrals) will have exactly the same issue as derivatives. But definite integrals do not. Should the variable of integration be scoped for definite integrals? What if one does Integrate[f[x], {x,0,x}]
? (Sounds contrived? I have received an endless slew of bug reports arising from just that scenario. I believe most are fixed.)
FourierTransform
is to some extent based on Integrate
. Should it scope locally, given that Integrate
does not? (Answer: I'm not certain, other than to say I don't foresee this changing.)
In contrast, NIntegrate
does scope its variable of integration, sort of, to the extent that the HoldAll
attribute prevents "premature evaluation" and allows the innards to do localized substitution. But it does not face a situation where the bounds of integration might not evaluate to numbers, let alone might evaluate to something depending on the integration variable. Well, they can do all that, but then it will issue an error and fail.