Setting class property types – take 2

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. 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. 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 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:

  1. The “@” symbol was replaced with a space
  2. The dimensionality and complexity cannot be specified

The new syntax has some drawbacks compared to the previous (undocumented) one:

  1. Backward compatibility – We can run the older (undocumented) syntax on any Matlab release since who-knows-when (at least as far back as R2010a [tested], and possibly older releases [untested]), including the very latest R2016a. On the other hand, the new (documented) syntax will only work on R2016a and will crash the program if you try to run it in older releases. This is not even something that you can catch with a try-catch block – the class will simply not load on any older Matlab release. If you need your code to run on older releases in addition to 16a, you have no choice other than to use the older syntax.
  2. Dimensionality – the new syntax, unlike the undocumented syntax, does not enable users to limit the data dimensionality (scalar/vector/array). This is a very important feature for program robustness and maintainability. Complexity is another type limitation that is missing, although it is less important than the dimensionality. And just in case you were wondering – the new syntax does not accept the additional scalar, vector, matrix and complex attributes like the older syntax; using them with the new syntax evokes an error.
  3. 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.
  4. Consistency – despite what I hoped, the new syntax still does not apply to function input args: we cannot (AFAIK) limit the input/output args of methods/functions in the same way that we can limit properties. If there’s a way to do this, I’d be delighted to learn (this comment may indicate that it is work in progress). It is true that this feature is not a drawback of the new syntax compared to the older one, since the old syntax didn’t have it either (AFAIK). But I would have expected a documented feature to be consistent across the Matlab language (or at least across the MCOS subset), and unfortunately the new feature fails this test.

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:

Don’t fix it if it ain’t broke!

Categories: Medium risk of breaking in future versions, Undocumented feature

Tags: , , ,

Bookmark and SharePrint Print

13 Responses to Setting class property types – take 2

  1. @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 following properties block

    OldStyle@char = 'old'
    NewStyle char = 'new'

    there is this interesting behavior:

    ›› obj.OldStyle = 65;
    While setting the 'OldStyle' property of 'Properties2016a': Value must be 'char'. 
     
    ›› obj.NewStyle = 65;
    ›› obj.NewStyle
    ans =
    A

    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:

    ›› m = metaclass(obj);
    ›› m.PropertyList(1).Type.Name
    ans =
    char
    ›› m.PropertyList(2).Type.Name
    ans =
    char property

    This also means that writing NewStyle@char property is equivalent to NewStyle 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. YishaiE says:

    Isn’t inputParser a solution to the function input typecheck issue (kludgy and convoluted, but functional)?

    • @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!

  3. Ulrik says:

    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.

  4. Egon says:

    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 StackOverflow.

    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, …:

    name : type = value

    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.

    • @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 folders containing class methods that are implemented as independent m-files, and also to call super-class methods from a sub-class.

  5. Adam says:

    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.

  6. Simon says:

    Matlab typing in general isn’t great. Try this;

    inceptDate datetime = datetime([],[],[]);  % The period start date as datetime

    Then set it like to the string –

    '1-Jan-2010'

    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…

  7. Chris Hoogeboom says:

    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

    classdef WeekDay
        enumeration
            Monday
            Tuesday
            Wednesday
            Thursday
            Friday
        end
    end

    Now I can define a class as such

    classdef DayExample
        properties
            day WeekDay = WeekDay.Monday
        end
    end

    and now I can do

    obj = DayExample();
    obj.day = WeekDay.Monday;
    obj.day = 'Tuesday';

    where in previous versions of MATLAB, I would have to implement it as

    classdef DayExampleLegacy01
        properties
            day@WeekDay = WeekDay.Monday
        end
     
        methods
            function this = set.day(this, d)
                if ischar(d) && isvector(d)
                    d = WeekDay.(d);
                end
                this.day = d;
            end
        end
    end

    or

    classdef DayExampleLegacy02
        properties
            day@char = 'Monday'
        end
     
        methods
            function this = set.day(this, d)
                WEEK_DAYS = {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'};
                assert(ismember(d, WEEK_DAYS), ...
                    'DayExampleLegacy02:IllegalDay', ...
                    '"%" is not a valid week day.', d);
                this.day = d;
            end
        end
    end
    • @Chris – nice to see you here!

      Your report chimes with Martin’s comment above. The new syntax’s documented automatic type-casting can be replicated in the old (undocumented @) syntax using the property attribute:

      classdef DayExample
          properties
              day@WeekDay property = WeekDay.Monday
          end
      end

      which allows setting the value as either an enumerated value or its char representation:

      >> d = DayExample
      d = 
        DayExample with properties:
       
          day: Monday
       
      >> d.day = WeekDay.Tuesday
      d = 
        DayExample with properties:
       
          day: Tuesday
       
      >> d.day = 'Thursday'
      d = 
        DayExample with properties:
       
          day: Thursday
  8. anonymous says:

    Actually, Matlab has relased a new syntax to define property restrictions (Size, Class, Validation Functions):

    https://de.mathworks.com/help/matlab/matlab_oop/validate-property-values.html

  9. Kyle says:

    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

    classdef Printer
        properties
           %graphicToPrint Graphic  % doesn't work
            graphicToPrint@Graphic
        end
        methods
            function print(this)
                % do some printing acting on graphicToPrint
                this.graphicToPrint.print();
            end
        end
    end
     
    classdef Graphic
        methods (Abstract)
            print(this);
        end
    end
     
    classdef ConcreteGraphic < Graphic
        methods
            function print(this)
                disp('print on ConcreteGraphic happened')
            end
        end
    end
     
    >> p = Printer;
    >> p.graphicToPrint = ConcreteGraphic;
    >> p.print
    print on ConcreteGraphic happened
    • Martin Lechner says:

      Hi Kyle,

      I normally initialize the property with

      properties
          graphicToPrint Graphic = ConcreteGraphic.empty
      end

      and have no problems with the new syntax.

Leave a Reply

Your email address will not be published. Required fields are marked *