필터 지우기
필터 지우기

(2018a) Implicit Call to Inherited Constructor is not available when used in a package

조회 수: 3 (최근 30일)
Hi All,
The goal is to try the new feature of Matlab 2018 using a package. In each of the examples there is a super class and a subclass. The subclass has no constructor but the superclass expects a input argument. In the case without a package this works fine as advertised in the release notes. In the case with package there is an error.
  댓글 수: 5
alex_7
alex_7 2018년 7월 10일
There is a limited number of class types, that means that I am not trying to create a class based on arbitrary string. The drawback of if-else statements is that if a new class type is available then another if statement has to be implemented. Anyway it seems that there is no solution to the problem and have to create constructors for the subclasses. Thank you very much for your time.
Adam
Adam 2018년 7월 10일
편집: Adam 2018년 7월 10일
What is wrong with the str2func approach if you really must do it this way?
Having to add an extra if-else clause may be seen as a drawback, but can also just be seen as part of extending a piece of tight code in an explicit way that forces you to ensure that the new case is satisfied. A very generic answer that can just create any type of class from any type of string may seem much nicer, but is also far more open to bugs and misuse, in the same way that just making every function and property of a class public makes it appear 'simpler' to use, but ultimately creates a mess.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2018년 7월 10일
I agree with Adam there are a lot of inconveniences that come with packages, so I've given up on them entirely.
I also completely agree with Adam, you should create a class factory. You can either implement that as a free function or as a static method of an abstract superclass of all your classes, or as a static method of a factory class.
The implementation of the creation code itself can also be done several ways as well, using if/else statements as Adam suggested or even with eval. That's one use of eval that would be perfectly valid. If all your classes derive from an abstract base you could even check that your string is indeed a valid class using meta.class. eval has the advantage that you indeed don't need to change anything when a new class gets added to the list.
And if the package issue gets fixed, you can switch to the package method suggested on StackOverflow if eval is disturbing.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2018년 7월 10일
편집: Sean de Wolski 2018년 7월 10일
I had to do this for an application I worked on where I wanted to have an unknown number of models in a package and to be able to scrape it and construct all classes that inherit from the same abstract base class as Guillaume describes.
This is also how the unit test framework creates test suites fromPackage or fromFolder (that's where I got the idea from).
methods (Access = protected)
% Figure out model trainers to use
function setModelTrainers(obj, modeltrainers)
if isempty(modeltrainers)
% Grab all classes in model trainers package
mt = meta.package.fromName('LoadForecaster.Prediction.ModelTrainers');
classes = mt.ClassList;
% Determine which ones are of the correct type
for ii = numel(classes):-1:1
switch obj.ModelType
case 'Regression'
idx(ii) = ismember(?LoadForecaster.Prediction.RegressionTrainer, classes(ii).SuperclassList);
% Add more cases to add classification, etc.
end
end
% Set
obj.ModelTrainers = classes(idx);
else
% Use passed in ones
obj.ModelTrainers = modeltrainers;
end
% Loop over model trainers building a function to the call the
% constructor.
for ii = numel(obj.ModelTrainers):-1:1
obj.ModelTrainerConstructors{ii} = str2func(obj.ModelTrainers(ii).Name);
end
end
end

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by