Comments on: Setting class property types – take 2 https://undocumentedmatlab.com/blog_old/setting-class-property-types-2 Charting Matlab's unsupported hidden underbelly Wed, 27 Nov 2019 11:18:32 +0000 hourly 1 https://wordpress.org/?v=4.4.1 By: Martin Lechnerhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-506236 Thu, 31 Oct 2019 11:59:04 +0000 https://undocumentedmatlab.com/?p=6390#comment-506236 Hi Kyle,

I normally initialize the property with

properties
    graphicToPrint Graphic = ConcreteGraphic.empty
end

and have no problems with the new syntax.

]]>
By: Kylehttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-504995 Wed, 23 Oct 2019 18:25:05 +0000 https://undocumentedmatlab.com/?p=6390#comment-504995 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
]]>
By: anonymoushttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-403034 Thu, 16 Mar 2017 09:46:10 +0000 https://undocumentedmatlab.com/?p=6390#comment-403034 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

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-381022 Wed, 22 Jun 2016 07:47:27 +0000 https://undocumentedmatlab.com/?p=6390#comment-381022 @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
]]>
By: Chris Hoogeboomhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-380655 Fri, 17 Jun 2016 12:30:49 +0000 https://undocumentedmatlab.com/?p=6390#comment-380655 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
]]>
By: Simonhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-375689 Wed, 04 May 2016 15:58:00 +0000 https://undocumentedmatlab.com/?p=6390#comment-375689 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…

]]>
By: Adamhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-375599 Tue, 03 May 2016 15:37:54 +0000 https://undocumentedmatlab.com/?p=6390#comment-375599 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.

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-375467 Sun, 01 May 2016 22:32:48 +0000 https://undocumentedmatlab.com/?p=6390#comment-375467 @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.

]]>
By: Egonhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-375438 Sun, 01 May 2016 12:28:25 +0000 https://undocumentedmatlab.com/?p=6390#comment-375438 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.

]]>
By: Yair Altmanhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-375379 Sat, 30 Apr 2016 17:25:13 +0000 https://undocumentedmatlab.com/?p=6390#comment-375379 @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!

]]>
By: Ulrikhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-375295 Fri, 29 Apr 2016 17:57:42 +0000 https://undocumentedmatlab.com/?p=6390#comment-375295 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.

]]>
By: YishaiEhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-375246 Fri, 29 Apr 2016 00:53:30 +0000 https://undocumentedmatlab.com/?p=6390#comment-375246 Isn’t inputParser a solution to the function input typecheck issue (kludgy and convoluted, but functional)?

]]>
By: Martin Afanasjewhttps://undocumentedmatlab.com/blog_old/setting-class-property-types-2#comment-375230 Thu, 28 Apr 2016 19:58:47 +0000 https://undocumentedmatlab.com/?p=6390#comment-375230 @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.

]]>