Why can't or why shouldn't Matlab "re-shadow" functions after eval() is called?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I know eval() is not to be encouraged. Nevertheless, I still wonder why errors like in the code below are still around after so many years and releases. Is it so problematic for Matlab to re-examine the list of variables in the workspace after eval() is issued and properly shadow any functions with name conflicts? I realize this might have to be less efficient than if eval() were absent, but why is it inevitable that an error be generated?
test
function test
eval('fft=1;');
whos fft
disp(fft)
end
0 commentaires
Réponses (1)
Walter Roberson
le 12 Nov 2021
Check the Language and Programming release notes for R2021a, "eval function". The warning is new transitional behaviour, and in some future release, it will be an error.
We are discussing efficiency in a different thread, in which you are suggesting removing some backwards compatible behaviour. This change removes some backwards compatible behaviour to improves efficiency.
8 commentaires
Stephen23
le 13 Nov 2021
Modifié(e) : Stephen23
le 13 Nov 2021
"Notice that the call to whos() successfully recognizes that fft is a double variable, so it is puzzling that disp cannot."
Your comparison of calling WHOS and some function call (as Steve Lord points out) is comparing apples and oranges. The documentation here
specifically advises to "Avoid functions that query the state of MATLAB such as inputname, which, whos, exist(var), and dbstack. Run-time introspection is computationally expensive": so what you ask for would require performing "computationally expensive" introspection (just like WHOS does now) to resolve the function every single time any function is called.
"The question in this thread is whether MATLAB could detect and avoid the name conflict created by eval if it wanted to."
As a design decision it certainly could, as WHOS proves... but it would have a significant time cost, just as WHOS does already. You basically have to throw out the JIT engine.
Voir également
Catégories
En savoir plus sur Debugging and Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!