Why can't or why shouldn't Matlab "re-shadow" functions after eval() is called?

2 vues (au cours des 30 derniers jours)
Matt J
Matt J le 1 Nov 2021
Modifié(e) : Matt J le 13 Nov 2021
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
Warning: Identifier 'fft' in the evaluated statement does not refer to the external function. In a future release, using an identifier different to how it is used in the file will not be supported.
Name Size Bytes Class Attributes fft 1x1 8 double
Error using fft
Not enough input arguments.

Error in solution>test (line 9)
disp(fft)
function test
eval('fft=1;');
whos fft
disp(fft)
end

Réponses (1)

Walter Roberson
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
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.
Matt J
Matt J le 13 Nov 2021
Modifié(e) : Matt J le 13 Nov 2021
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.
Why "any function"? If it's already possible to confine the repercusions to select function calls like WHOS, then why not for EVAL as well?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by