- Undocumented Matlab - https://undocumentedmatlab.com -
Setting class property types – take 2
Posted By Yair Altman On April 27, 2016 | 11 Comments
Three years ago, almost to the day, I wrote about a very handy undocumented feature of Matlab classes that enables us to specify type restrictions for any Matlab class property [1]. We can specify property type (for example, char
, double
or any Matlab class) as well as dimensionality (scalar
, vector
, or matrix
) and complexity indication (complex
). Doing so has multiple benefits for code performance, robustness and maintainability. For example:
% Undocumented syntax - works well since at least R2010a (possibly earlier)
classdef Packet
properties
PacketType@char
HeaderLength@uint16
PayloadLength@uint16 scalar = uint16(0); % initial value
PacketData@uint8 vector
end
end
In the recent release of Matlab R2016a, a similar feature have finally become fully supported and documented [2]. The corresponding snippet above would look something like this:
% Documented syntax - only works in R2016a or newer
classdef Packet
properties
PacketType char
HeaderLength uint16
PayloadLength uint16 = uint16(0); % initial value
PacketData uint8
end
end
Unfortunately, I dislike the new documented functionality, so I didn’t feel like promoting it in this blog when it came out. But since a blog reader mentioned it [3] a few days ago, I wanted to come out publicly with my opinion and a detailed explanation.
If you look closely at the code snippets above, you will notice two important differences:
The new syntax has some drawbacks compared to the previous (undocumented) one:
scalar
, vector
, matrix
and complex
attributes like the older syntax; using them with the new syntax evokes an error.In fact, aside from the fact that the new syntax is documented, I can see no advantages that it offers over the older syntax, only disadvantages. Or am I missing something? Please do tell if you see any important advantages that I’ve missed.
Luckily for us, the old syntax remains operational, side-by-side with the new one. This enables us to keep running our existing code without worrying [too much] that it might break in R2016a. Maybe the new syntax will grow on me (or improve) in upcoming years, but for the time being I see no benefit in switching away from the @ syntax.
For the past few years, I hoped that the property typing feature will become documented and that it will be a continuation of the undocumented syntax rather than what eventually aired. I’m afraid it’s too late to revert it now that it has… Realistically speaking, the best we can hope for now is for the older syntax to remain operational, and not be withdrawn in some future Matlab release. Making the undocumented syntax documented as-is would be great, but I’m afraid it is unrealistic given the new circumstances.
I’m sorry if I take the wind off MathWorks’ sails a bit here, but MathWorks knows that it can count on me to speak my mind without bullshit. Sometimes for the good, sometimes not. All in good spirit and the common interest of improving Matlab over time. No offence intended – it’s just my personal opinion after all.
In my opinion this is one of those rare cases where the developers obviously intended to make something better but eventually came out with something worse. They should have stuck to what was. After all, the first and foremost rule of engineering is, and always was:
Categories: Medium risk of breaking in future versions, Undocumented feature
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/setting-class-property-types-2
URLs in this post:
[1] type restrictions for any Matlab class property: http://undocumentedmatlab.com/blog/setting-class-property-types
[2] documented: http://www.mathworks.com/help/matlab/matlab_oop/restrict-class-of-properties.html
[3] mentioned it: http://undocumentedmatlab.com/blog/setting-class-property-types#comment-374984
[4] this comment: http://undocumentedmatlab.com/blog/setting-class-property-types#comment-343150
[5] Setting class property types : https://undocumentedmatlab.com/articles/setting-class-property-types
[6] Handle object as default class property value : https://undocumentedmatlab.com/articles/handle-object-as-default-class-property-value
[7] Getting default HG property values : https://undocumentedmatlab.com/articles/getting-default-hg-property-values
[8] Types of undocumented Matlab aspects : https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects
[9] Undocumented plot marker types : https://undocumentedmatlab.com/articles/undocumented-plot-marker-types
[10] Class object tab completion & improper field names : https://undocumentedmatlab.com/articles/class-object-tab-completion-and-improper-field-names
[11] : http://stackoverflow.com/questions/1712274/why-do-a-lot-of-programming-languages-put-the-type-after-the-variable-name
[12] : http://www.mathworks.com/help/matlab/matlab_oop/organizing-classes-in-folders.html
[13] : http://www.mathworks.com/help/matlab/matlab_oop/calling-superclass-methods-on-subclass-objects.html
[14] : https://undocumentedmatlab.com/blog/setting-class-property-types-2#comment-375230
[15] : https://de.mathworks.com/help/matlab/matlab_oop/validate-property-values.html
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
11 Comments To "Setting class property types – take 2"
#1 Comment By Martin Afanasjew On April 28, 2016 @ 22:58
@Yair – Addressing your “In fact, aside from the fact that the new syntax is documented, I can see no advantages that it offers over the older syntax, only disadvantages. Or am I missing something? Please do tell if you see any important advantages that I’ve missed.”
Some may see it as an advantage, but I think the following is making things even worse. In addition to different syntax there is also different semantics of the old (undocumented) and new property type declarations. Assuming
obj
is an instance of a class with the followingproperties
blockthere is this interesting behavior:
This means the new syntax automatically converts to the declared type, if possible. (To be fair, this is documented.) The old syntax is more strict and throws an error on type mismatch, something I find much more desirable.
The following transcript reveals the difference in internal representation:
This also means that writing
NewStyle@char property
is equivalent toNewStyle char
and will give the new behavior using the old syntax. All of this feels pretty convoluted and like you I fail to see the benefit of creating yet another syntax instead of documenting (and extending) the old syntax that has existed for a long time.#2 Comment By YishaiE On April 29, 2016 @ 03:53
Isn’t inputParser a solution to the function input typecheck issue (kludgy and convoluted, but functional)?
#3 Comment By Yair Altman On April 30, 2016 @ 20:25
@YishaiE – inputParser is simply a function that checks the validity inputs. It is essentially no different than using other programmatic solutions such as assert or isa() etc. The potential benefits of constricting the input args at the function declaration level is that it would avoid the need to add these programmatic checks, and also the overheads associated with the function/method call in case of a type mismatch. Moreover, the code becomes cleaner, smaller, more robust and more maintainable. These are basically the same benefits of constricting property types.
Hag Same’ach!
#4 Comment By Ulrik On April 29, 2016 @ 20:57
Just a warning:
The @ for properties only works well for built-in objects in the older versions of Matlab.
If you use the @ for custom classes you can crash Matlab if the path to the classdef becomes unknown (e.g. you have not set it using addpath or the path is modified in a bad way).
Some of the built-in stuff in Matlab is written a bit “alternative” (e.g. uigetdir) and can trigger and a bug where the Matlab UI path goes out of sync with the actual path.
The bug seems to be (Matlab internal) event related.
Now with the new functionality I am looking forward to testing if this bug has been fixed or not. If not Mathworks can expect a bug-report.
#5 Comment By Egon On May 1, 2016 @ 15:28
Cross compatibility – it is very confusing to users coming to Matlab from other programming languages, all of which (without any important exception) place the type name to the LEFT of the identifier name, not to its RIGHT. People coding in both Matlab and Java/C/C++ would easily get confused and frustrated.
I disagree with this. There are quite a few languages that annotate the type to the right of variable names. Just have a look at Pascal (for an old language), and newer (and popular) languages such as Scala, Swift, Go, Rust, …: all of those define the type after the variable name.
Technically, this simplifies semantic analysis and is easier when type inference is needed.
From a human point of view, I also think it makes more sense. Specifically: the important part is the name of a variable (what does it mean?) rather than its type (implementation detail).
More info also at [11].
I hence really like that in MATLAB the type declaration is after the field name (no need to replicate the behavior of languages such as C that were explicitly written to be really concise and have quite some peculiarities as a result: e.g. defining both a variable and a pointer on the same line is possible but confusing).
On the other hand, I fully agree with your main point that MATLAB should have just documented the old syntax and not create new ones that are not backwards compatible. But if I’m completely honest, I would rather have seen the syntax as in Pascal, Swift, Scala, …:
where the :type part is optional from the start.
I particularly dislike how the old syntax uses @while those types don’t have an obvious link to function handles, and how this only works for properties, not for function/method arguments.
#6 Comment By Yair Altman On May 2, 2016 @ 01:32
@Egon – surely you are not equating the importance of Pascal (which I love, don’t get me wrong) et al, with that of C/C++/C#/Java. By whichever measure that you choose, these 4 latter languages far outweigh the others in importance.
Moreover, even the sample languages that you cite use the : operator to separate the identifier from its type, and in this respect they are much closer to Matlab’s old (undocumented) syntax that uses the @ operator.
Finally, the @ syntax is actually consistent with Matlab classes: after all, Matlab classes use @ to indicate [12] that are implemented as independent m-files, and also to [13] from a sub-class.
#7 Comment By Adam On May 3, 2016 @ 18:37
Thanks, this is certainly interesting. More so for me because I somehow missed your previous post from 3 years ago and knew nothing about the undocumented syntax (or the new documented one) previously! I did see some new appdesigner code recently though which included this new syntax and surprised me since I had never seen it before and thought it must somehow be special for appdesigner-produced code.
I have been using assignments to e.g. someClass.empty in a properties block to act mostly as documentation about what is expected and help with certain array-based initialisations and always use a validateattributes block in property set functions, but using this syntax (well, the undocumented older one at least) is potentially something I will look at for new classes.
#8 Comment By Simon On May 4, 2016 @ 18:58
Matlab typing in general isn’t great. Try this;
Then set it like to the string –
Matlab accepts this in both old and new ‘type checking’ formats. If you retrieve the inceptdate variable, it’s a character array, not a datetime.
Functionally then, you *still* need to roll your own type checking, whether you use the old or new style.
For small projects this is okay. For larger living projects, not having type safety is terrible…
#9 Comment By Chris Hoogeboom On June 17, 2016 @ 15:30
Hi Yair,
I agree with you on all of your points, especially with regards to the backwards compatibility issue. One nice thing that I noticed while reading through the documentation is that you can now specify properties to be a part of an enumeration class, which then allows you to set those properties as strings. For instance, suppose I have an enumeration
Now I can define a class as such
and now I can do
where in previous versions of MATLAB, I would have to implement it as
or
#10 Comment By Yair Altman On June 22, 2016 @ 10:47
@Chris – nice to see you here!
Your report chimes with [14]. The new syntax’s documented automatic type-casting can be replicated in the old (undocumented @) syntax using the
property
attribute:which allows setting the value as either an enumerated value or its char representation:
#11 Comment By anonymous On March 16, 2017 @ 11:46
Actually, Matlab has relased a new syntax to define property restrictions (Size, Class, Validation Functions):
[15]
#12 Comment By Kyle On October 23, 2019 @ 21:25
I have serious issues with the new syntax actually. If you want to have abstract classes, the old method would work. The new method does not. For example the below 3 classes work with the old way, but the new way does not. Also sorry for the formatting. I couldn’t seem to indent and add spaces properly
#13 Comment By Martin Lechner On October 31, 2019 @ 13:59
Hi Kyle,
I normally initialize the property with
and have no problems with the new syntax.