Can I use Matlab's default implementation of empty with matlab.mix​in.indexin​g.Redefine​sParen?

12 views (last 30 days)
I'm trying to use the new method of overloading indexing with the matlab.mixin.indexing.RedefinesParen class, new in 2021b. However, I like and am fine with Matlab's default implementation of empty, which becomes Abstract and requires definition in classes that inherit from RedefinesParen. In fact, I'm making a general top-level class and I'm trying to make things easy on developers, so I like that the default implementation of empty for classes doesn't have to worry about property validation, which is sometimes defined in the constructor rather than with the property definition.
Overloading empty and choosing an "empty" property that will pass through error-checking in the constructor is an extra layer of worry that I am hoping developers will not have to worry about when inheriting from this class. Is there any way to use the default empty that Matlab implements behind-the-scenes for classes that inherit from RedefinesParen or am I out-of-luck on this front?

Answers (1)

James Lebak
James Lebak on 27 Apr 2022
You do have to provide an 'empty' for your class that inherits from RedefinesParen. Such classes are scalars masquerading as arrays, and MATLAB unfortunately can't guarantee that its default empty will be functionally correct in all cases for such classes.
If you provide your own implementation of 'empty', then classes that inherit from your class should not need to do anything extra.
  3 Comments
James Lebak
James Lebak on 17 Jul 2023
Empty takes size arguments, either a size vector or a set of scalars:
As specified in the documentation, at least one size parameter is expected to be zero when calling the empty method.
Classes that inherit from RedefinesParen are scalar objects masquerading as arrays. As such the usual implementation is that there are one or more properties in the object that store array data. The idea of the empty method is to construct those properties with the sizes passed to the empty method. We provide a couple of examples:
It is true that varargin does not give you any information on subclasses in this context. If you need such information to correctly construct an empty of a given subclass then empty does have to be defined by the subclass.
Robin
Robin on 24 Jul 2023
Thanks James for the elaboration!
Unfortunately, my class is not a scalar object masquerading as an array and I have dozens of subclasses for the class that I am defining the empty method for, so defining it in each of those is not an option. I will see if using good ol' subsasgn is going to work for me.

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!