Dynamic import with evalin not possible

3 次查看(过去 30 天)
Hi,
My previous question (link) led me to a conclusion that the only way to allow calling of static methods from within the same classdef was to introduce a self-import statement at the beginning of each static method, which would in turn import the class along with the surrounding pacakge structure into the scope of the static method (seems very redundant and broken by design).
However, I ran across yet another weird behavior, namely that evalin("caller","import ...") imports into the current scope instead of the caller's scope. This is true for methods, functions, as well as scripts. For example:
function testImport
! mkdir +TestPackage
x = 'main';
fprintf("Outside subfunction import list count: %d\n",length(import));
fprintf("Outside subfunction normal variable: %s\n\n",x);
testsubf()
fprintf("\nOutside subfunction import list count: %d (should increase by one)\n",length(import));
fprintf("Outside subfunction normal variable: %s\n",x);
function testsubf()
fprintf("Before subfunction evalin import list count: %d\n",length(import));
fprintf("Before subfunction evalin normal variable: %s\n",x);
evalin("caller","import +TestPackage.*")
evalin("caller","x = 'sub';")
fprintf("After subfunction evalin import list count: %d (should remain the same)\n",length(import));
fprintf("After subfunction evalin normal variable: %s\n",x);
end
end
produces an output:
>> testImport
Outside subfunction import list count: 0
Outside subfunction normal variable: main
Before subfunction evalin import list count: 0
Before subfunction evalin normal variable: main
After subfunction evalin import list count: 1 (should remain the same)
After subfunction evalin normal variable: sub
Outside subfunction import list count: 0 (should increase by one)
Outside subfunction normal variable: sub
I have found a very similar unuanswered issue but regarding evalin("base",...) here.
Is there any way to solve this? The above behavior makes it harded to dynamically import packages, which would be especially helpful to make functions/classess independent of the structure of packages in which they're located.
On top of that, I see that "in a future release [I use R2022b], IMPORT will not accept variable names, function calls, or operators [...]", making it necessary to do eval("import ...") even in case of simple dynamic import to local scope (which currently can still be done with import(pkg) call). I sincerely hope that eval("import ...") will still be possible or else each packaged function or static method will have to have the package prefix hard-coded in front of each call.
  1 个评论
Robert Borkowski
Robert Borkowski 2022-12-13
The above is also misbehaving if I nest eval("import +TestPackage.*") inside of evalin:
evalin("caller","eval(""import TestPackage.*"")")

请先登录,再进行评论。

回答(1 个)

Neha
Neha 2023-9-7
Hi Robert,
I understand that you want to dynamically import packages into the caller’s scope. "evalin" is only used for evaluating in a specific workspace. Local/nested functions and imports, which are specific to another frame's (like the caller's) context, are inaccessible. This is the documented behaviour. You can refer to the following documentation link for more information on the "evalin" function:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Install Products 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by