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
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 个)
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Install Products 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!