How to make calls to static methods independent of packaging structure (name)?
5 次查看(过去 30 天)
显示 更早的评论
Hello,
I have the following class:
classdef TestStaticMethodPackage
methods (Static)
function hello
fprintf("Hello world\n");
end
function write
TestStaticMethodPackage.hello;
end
end
end
I run it:
>> TestStaticMethodPackage.write
Hello world
I am now trying to organize my code into a package:
>> !mkdir +TestPackage
>> !move TestStaticMethodPackage.m +TestPackage\
Trying to run now:
>> TestPackage.TestStaticMethodPackage.write
Unable to resolve the name 'TestStaticMethodPackage.hello'.
Error in TestPackage.TestStaticMethodPackage.write (line 9)
TestStaticMethodPackage.hello;
This behavior breaks essentially all static methods, and requires the classes to be aware of their external environment. Is there a simple workaround to make it flexible to work in any package structure without having to change all calls where static methods are used?
I was thinking about writing some static method launcher based on dbstack and eval but what's making things worse is that dbstack does not see the package name in front of the class name. Adding the following line before TestStaticMethodPackage.hello; line in class definition:
s = dbstack('-completenames')
results in:
s =
struct with fields:
file: 'C:\...\+TestPackage\TestStaticMethodPackage.m'
name: 'TestStaticMethodPackage.write'
line: 9
'name' seems inconsistent, since even within the class itself I need to prepend package name to class name to call a static method.
2 个评论
Jiri Hajek
2022-12-14
Hi, it seems that you want to run code that is not on the MATLAB path. It is not clear why you should want this, as you can easily "addpath", if its really important for you. You want to build a compiled app and be able to run it anyway or what is the purpose of this?
Also, you are using funny name for your directory (note the "+" sign within the folder name. Matlab is generally sensitive about special characters on the path.
回答(2 个)
chrisw23
2022-12-13
try to set a session path (addpath) and import the package path (i.e. import MyPackageRoot.TestPackage.*)
2 个评论
chrisw23
2022-12-16
编辑:chrisw23
2022-12-16
Path ..MatLabClassExample\+TestPackage\@TestStaticLibrary
TestScript at MatLabClassExample dir
import TestPackage.*
str = "direct static call: " + TestStaticLibrary.hello()
myStaticLib = TestStaticLibrary();
str = "indirect static call: " + myStaticLib.write()
classdef TestStaticLibrary < handle
events
% requires handle super class
end
properties (SetAccess = public, GetAccess = public)
% to be defined
end
methods (Access = public)
function obj = TestStaticLibrary()
% construcor
end
function delete(~) % ~ replaces unused obj argument here
% destructor
end
%% my public methods
function str = write(obj)
str = obj.hello;
end
end
%% private methods
methods (Access = private)
end
%% static methods
methods (Static)
function str = hello
str = "Hello world";
end
end
end
Walter Roberson
2022-12-16
Known work-arounds:
- preprocessing the source code to convert some kind of relative reference to absolute reference before executing
- pass down the package names in calls so that you can dynamically generate the import statements (does not play well with compilation)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!