Why can't or why shouldn't Matlab "re-shadow" functions after eval() is called?
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Walter Roberson
2021 年 11 月 12 日
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 件のコメント
Stephen23
2021 年 11 月 13 日
編集済み: Stephen23
2021 年 11 月 13 日
"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.
参考
カテゴリ
Help Center および File Exchange で Debugging and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!