Bamboo
Bamboo

Reputation: 973

Validate property to be a sublclass of an abstract class in MATLAB

I'm brand new to OOP in Matlab, and still fairly green when it comes to OOP in general, but what I do know I learnt in C++.

I'm following the Matlab documentation found here Property class and size validation. I want to validate a property so that it must be a specific class and I'm using the example from the link. This is what my class looks like:

classdef simpoint
   ...
   properties
      ...
      outputType dataType
      ...
   end
   ...
end

In my code dataType is a class I've written. What's more it's abstract.

I'm getting the error

Error defining property 'outputType' of class 'simpoint':
Class dataType is abstract. Specify a default value for property outputType.

The class dataType is abstract to force the user to implement some methods. I'm trying to use property validation to make sure when outputType is set, the class is a subclass of dataType.

I don't really want to set a default value, because forgetting to set outputType should throw an error.

How can I validate outputType to make sure it is a subclass of dataType? Is there a better way to do this in Matlab?

Upvotes: 3

Views: 854

Answers (2)

dezlov
dezlov

Reputation: 860

There is a more elegant solution to this problem, which is apparently not well known.

MATLAB has a concept of Heterogeneous Class Hierarchies. This is just fancy way of explicitly declaring the common root class (abstract or not) so that it can be used for property validation. In practice, all you need to do is to make your abstract class inherit from matlab.mixin.Heterogeneous.

Here is a quick example:

classdef (Abstract) AbstractItem < handle & matlab.mixin.Heterogeneous
end
classdef Collection < handle
    properties
        items AbstractItem
    end
end

Then you have no problem:

>> x = Collection

x = 

  Collection with properties:

    items: [0×0 AbstractItem]

Without the matlab.mixin.Heterogeneous inheritance you would get an error like you described:

Error defining property 'items' of class 'Collection'. Class AbstractItem is abstract. Specify a default value for property items.

Upvotes: 6

Wolfie
Wolfie

Reputation: 30165

Your current code uses the following logic:

  1. Create a new simpoint object
  2. Ah this object needs an outputType property
  3. Initialise the outputType property to be an empty dataType object
  4. Uhoh, we can't instantiate an abstract object - error.

Instead, you could also use setters and getters to validate data types. This removes steps 3 and 4 above, since the initial property value will be [].

classdef simpoint < matlab.mixin.SetGet
    properties
        outputType
    end
    methods 
        % ...
    end
    methods % Setters and getters
        function set.outputType( obj, v )
            % When the 'obj.outputType = X' is called, this function is
            % triggered. We can validate the input first
            assert( isa( v, 'dataType' ) );
            % If the assertion didn't error, we can set the property
            obj.outputType = v;
        end
        function v = get.outputType( obj )
            % Nothing bespoke in the getter (no not strictly needed), just return the value
            v = obj.outputType;
        end
    end
end

For more informative validation, you could use validateattributes instead of assert.

In this case, the default value of outputType will be [] unless you initialise it in the constructor.

Note, by using matlab.mixin.SetGet to enable setters and getters, I've implicitly made your object a handle. In broader OOP terms, the object is now accessed "by reference" rather than "by value". Read more here.

If you don't want a handle then you can remove the < matlab.mixin.SetGet and, by your own comment, define the setter more explicitly

function obj = set.outputType( obj, v )
    % Have to return 'obj' if the class isn't a handle.
    % ...
end

Upvotes: 2

Related Questions