How to make calls to static methods independent of packaging structure (name)?
조회 수: 4 (최근 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!