Misleading wording in the documentation on Matlab package (+) folders.

10 ビュー (過去 30 日間)
Kris Janssen
Kris Janssen 2016 年 4 月 21 日
移動済み: Dyuman Joshi 2024 年 1 月 4 日
In the page Packages Create Namespaces there are two sections on how to reference package members:
  • From inside the package
  • From outside
When considering the 'within' case, there is an explicit statement:
Functions, classes, and other packages contained in a package are scoped to that package
Further considering how namespaces work in other languages I would have thought that within a package, it would not be necessary to use the package prefix to call other members of a package. Unfortunately however, this seems not to be the case.
+mypack\functionOne.m +mypack\functionTwo.m
If I want to call functionOne from functionTwo, I still have to use mypack.functionOne()
Why is this?? Am I missing something because this just seems very unpractical and makes me wonder about the meaning of the statement:
Functions, classes, and other packages contained in a package are scoped to that package
  3 件のコメント
Jim Svensson
Jim Svensson 2019 年 1 月 18 日
移動済み: Dyuman Joshi 2024 年 1 月 4 日
I just concur that this is one of the top stupidest design desisions of the Matlab language.
Dariusz Borkowski
Dariusz Borkowski 2019 年 12 月 6 日
移動済み: Dyuman Joshi 2024 年 1 月 4 日
I can confirm. The current package design is a no-go.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2016 年 4 月 21 日
It does say in the documentation that you've linked under Referencing Package Members Within Packages that "All references to packages, functions, and classes in the package must use the package name prefix, unless you import the package".
In my opinion, it's a very stupid decision by Mathworks that completely kills the usefulness of packages. Rename the package and you've got to edit every single file within the package that references other files in the package.
It also stop the user of your code to simply wrap it into a package name of his choice (that he knows is not going to conflict with another package name) since the user is then force to edit your code to add references to the package name. So, this just moved the problem of you having to choose a name for your function that is not going to conflict with somebody's else functions to you having to choose a name for a package that is going to conflict with somebody's else package.
  2 件のコメント
Kris Janssen
Kris Janssen 2016 年 4 月 21 日
OK, so it is not only me...
Coming from C#, Java, ... This is indeed a bit of a hard pill to swallow.
I was in the process of developing a modular system to quickly compose apps, like e.g. PRISM in .NET (but simpler) but not being able to conveniently group stuff in packages without all this verbosity is just a pain.
It would have been slightly ok if import statements could be made at the class level but also that is only supported at function level. An equally incomprehensible decision.
covariant_cat
covariant_cat 2018 年 6 月 5 日
編集済み: covariant_cat 2018 年 6 月 5 日
Same problem here.
This is a very bad design since why should the code within a package depend on the package's name? What if I want to change the package name in the future (or put some of the code into another package)? Don't I have to modify every function in the package in that case?
Related questions:

サインインしてコメントする。

その他の回答 (2 件)

Miron Voin
Miron Voin 2020 年 3 月 22 日
This behavior seems to be unacceptable - looks just like a bug. Any workaround from Mathworks?
  1 件のコメント
Nathan Zimmerberg
Nathan Zimmerberg 2020 年 3 月 31 日
This is a workaround I came up with, it works on my Mac os 2019b MATLAB.
[filepath,~,~] = fileparts(mfilename('fullpath'));
packages= split(string(filepath),filesep+"+")';
for i= 2:length(packages)
import(join(packages(2:i),".")+".*")
end
I add this to the begining of all of my functions, and it just imports all the parent packages. I have no idea what effect this might have on performance, and there is a warning that importing using variables will no longer be supported in a future version of MATLAB, but hopefully mathworks will create a better workaround by then.

サインインしてコメントする。


jg
jg 2020 年 4 月 22 日
編集済み: jg 2020 年 4 月 22 日
Ouch, I knew I wasn't only one that thinks this is a mess. The main benefit of packages is that members of the package don't have to refer to other members of the same package by using the package name.
That's like addressing everyone in your immediate family using Firstname Middlename Lastname every time you talk to them.

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by