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

2 ビュー (過去 30 日間)
Matt J
Matt J 2021 年 11 月 1 日
編集済み: Matt J 2021 年 11 月 13 日
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

回答 (1 件)

Walter Roberson
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
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.
Matt J
Matt J 2021 年 11 月 13 日
編集済み: Matt J 2021 年 11 月 13 日
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?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by