Why not possible to use property class validation with abstract class?

3 views (last 30 days)
Jim Svensson
Jim Svensson on 4 Nov 2021
In the code below class fruit_t has an abstract method that is implemented by class banana_t.
I can not instantiate a fruit_t, but I can instantiate a banana_t.
But why can I not use fuit_t for property class validation in another top class top_t?
Error message is
Error defining property 'fruit' of class 'top_t'. Class fruit_t is abstract. Specify a default value for
property fruit.
As property fruit is a "immutable" property of top_t it will be set in the contructor, so why does Matlab need another default value?
If fruit_t is not abstract and fruit is not assigned a value in the constructur then property fruit will be of type
fruit: [0×0 fruit_t]
Why can't matlab think the same way with abstract classes then, since there is not created instance of fruit_t.
My suggestion:
  1. Either don't require a "default value" for immutable properties (because they will be set in the constructor), or
  2. Allow zero element arrays of abstract classes (since there are no instances, there is no problem).
classdef top_t < handle
properties (SetAccess = immutable)
fruit fruit_t
end
methods
function e = top_t()
e.fruit = banana_t();
end
end
end
classdef fruit_t < handle
methods (Abstract)
y = foo(x);
end
end
classdef banana_t < fruit_t
methods
function y = foo(e, x)
y = 2*x;
end
end
end

Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by