Overloading package-scoped custom functions with class methods

I am looking for a way to overload a package-scoped function with a class method.
My team has developed a package containing several custom functions that are useful for many of our teams.
The teams, however, may be using classes where they wish to overload some of the package functions with methods in their class.
I can already overload an inbuilt MATLAB function with a method in a class. I want to be able to overload custom functions from the package (since all their teams may now be using and importing these custom functions through a package, and it would be good to define how those custom functions apply to custom objects).
How can I properly overload package functions with class methods?

 採用された回答

MathWorks Support Team
MathWorks Support Team 2022 年 3 月 29 日

0 投票

When calling a class method that has a naming conflict with a package function of the same name, MATLAB applies "import" and finds the package function first. It is recommended to not use a package in cases where you have name conflicts and plan to import the package.
Potential workarounds, however, are:
  1. To use static methods in the class for any methods with name conflicts with the package custom functions. Static methods are associated with a class, but not with specific instances of that class. These methods do not require an object of the class as an input argument. Static methods, however, will need to be called using dot-notation in reference to either the "className" of an object which is an instance of that class.
    1. e.g. className.methodName() or obj.methodName() where "obj" is a "className" object.
  2. Add an "ismethod" check in the implementation of the package function with the naming conflict and have that check result in a call to the object's method of the same name if it exists.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeClass Introspection and Metadata についてさらに検索

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by