In MATLAB, inheriting from matlab.mixin.Heterogeneous
for the base class will allow nice polymorphism. However, it appears this isn't supported in Coder. First, can anyone confirm that Coder cannot use matlab.mixin.Heterogeneous, and if not, does anyone have a nice workaround?
For instance, I have the usual setup of an ABC called Shape, which is derived from both handle and matlab.mixin.Heterogeneous. Then I have Rectangle and Circle derived from Shape. I'd like to be able to have an array like:
classdef Shape < handle & matlab.mixin.Heterogeneous
classdef Circle < Shape
classdef Rectangle < Shape
harray = [Rectangle(height, width), Circle(radius)];
In Matlab, harray becomes a type "1x2 heterogeneous Shape (Rectangle, Circle) array
"
In Coder, I get the error "Base class 'matlab.mixin.Heterogeneous' cannot be loaded.
" during the build.
Any ideas how this could be approached? Before I do something crazy, I just want to check if there's something obvious I'm missing.
You are correct that matlab.mixin.Heterogeneous
is not supported for code generation. I reported your desire to use it and the unclear error message to the MATLAB Coder team.
Arrays of objects are not supported for code generation (see bullet 3 at that link). If you try:
[Circle(1), Circle(1)]
you'll get an error stating that fact:
??? Arrays of objects are not supported for code generation.
matlab.mixin.Heterogeneous
likely wouldn't have much utility in code generation without such support.