No way to add a package as a dependency of more than one other package?

조회 수: 1 (최근 30일)
Danny Kumpf
Danny Kumpf 2020년 12월 1일
I just want to confirm that this is really an issue with MATLAB, and not with me.
Let's say I have a package +pkg1 that is a dependency of two other packages: +pkg2, and +pkg3. And let's say that some functions in +pkg1 rely on other +pkg1 functions; that is, say we have +pkg1/fn1.m, and this function needs to call +pkg1/fn2.m. The problem is that once I place +pkg1 into the +pkg2 folder, I now need to go in and change how pkg1 calls its own internal functions! Whereas before, fn1.m had the line "pkg1.fn2()", now if I place +pkg1 into +pkg2, I need to change this line to "pkg2.pkg1.fn2()". And now if I want to place the same +pkg1 into +pkg3, I'm stuck. I would need to modify pkg1 independently for use in pkg2 and pkg3, which is ugly and sometimes impossible.
There is no built-in solution to this problem in MATLAB, correct? There is no way to add the same exact code (say, the same git branch of +pkg1) as a dependency of two different packages?
I have found a hacky solution that works under certain circumstances: Paste the following line to be the first line of every function within +pkg1 that calls another pkg1 function:
eval(sprintf('import %s.*', strjoin(regexp(mfilename('fullpath'), '(?<=+)\w*', 'match'), '.')));
This line:
  • Gets the full file path, eg C:\repos\+pkg2\+pkg1\fn1.m
  • Uses regexp to find all packages on the path, ie `+<pkg_name>` sub-strings
  • Joins the package names together with '.', eg: `pkg2.pkg1`
  • Uses `eval()` to call matlab's `import` statement: `import pkg2.pkg1.*`
After executing, you can call any functions that exist in pkg1 directly, without including the package names at all (eg, call just fn2()). This means that, from within a pkg1 function, you don't need to know whether pkg1 is a package, or whether it is a sub-package of any other package(s). This even works even if pkg1 itself is NOT a package, but instead is just added to the matlab path (because `import .*` does nothing). With this line added, you could add the exact same version of +pkg1 to both +pkg2 and +pkg3. But it's ugly, and doesn't always work - for example, if you have a classdef in +pkg1, and the property declarations call other functions in the class, I don't think there's any way to execute this import statement before you need to access those class functions.
Am I missing something that makes all this possible and easy?

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by