How do I encapsulate an old-style class with a classdef class?

조회 수: 8 (최근 30일)
Bob Clark
Bob Clark 2018년 10월 10일
댓글: Bob Clark 2018년 10월 11일
I'm re-writing one of the objects in our obj = class(obj,<blah>); hierarchy to classdef syntax. But I only want to rehash ONE class (we have 26, and some of them are... 'of notable size'). The class up for conversion "has a" relationship with two other classes still defined in the old syntax.
The situation looks like this:
Contents of +newfangled\zippy.m:
classdef zippy
% ZIPPY nifty shiny
properties
% elegant 'has-a' syntax, but validate engine won't shut up.
spam(5,1) grumpy
end % public properties
end % classdef
>> eggs = zippy
Error defining property 'spam' of class 'newfangled.zippy':
Class named 'grumpy' is undefined or does not support property validation.
>> which grumpy
C:\<blah>\<yadda>\<etc>\@grumpy\grumpy.m
Which contains roughly:
function obj = grumpy(varargin)
switch (nargin)
case 0
obj.noneyer = true;
obj.comments = 'business.';
obj = class(obj,'grumpy');
case 1
obj = digSumptinUp(varargin{1})
otherwise
error('Harumph.');
end % blade
end % classy grumpy constructor
Is there a way to get the classdef 'validation engine' to %#<INVLDTYPEOK> a parameter?
>> lasterror
ans =
struct with fields:
message: 'Error defining property 'grumpy' of class 'newfangled.zippy':...'
identifier: 'MATLAB:class:InvalidType'
stack: [0x1 struct]
So it has a name--but how do I wrap a default constructor in an internal try-catch block to shove 'MATLAB:class:InvalidType' down a hole?
So I tried:
classdef zippy
% ZIPPY do dah
properties
% type defaults to double
spam
end % public properties
methods
function obj = zippy(varargin)
spam = grumpy; % late-bind hack-- but won't coerce spam.
end % constructor
end % public methods
end % classdef
>> eggs = zippy
The following error occurred converting from grumpy to double:
Conversion to double from grumpy is not possible.
And sure, coercion is a RHS thing. But is there a way to re-assign the parameter class late, or create the parameter entirely in the constructor (like the old syntax does)?
Oh wait-- can I...
classdef zippy
% ZIPPY funcy
properties
% anonymous pass-through. Parameter is a function handle.
spam @(ham) grumpy (ham)
end % public properties
end % classdef
>> effs = zippy
Unexpected MATLAB operator
...because grumpy isn't really a function, it's a class. But you can see where I'm going with this. Can I even make a parameter of type function_handle?
classdef zippy
% ZIPPY funcy
properties
% anonymous pass-through. Parameter is a function handle.
spam function_handle
end % public properties
end % classdef
>> effg = zippy
Error defining property 'spam' of class 'newfangled.zippy':
Unable to construct default object of class function_handle.
Again: the validator won't shut up.
>> lasterror
struct with fields:
message: 'Error defining property 'spam' of class 'newfangled.zippy':...'
identifier: 'MATLAB:class:DefaultPropertyValueRequired'
stack: [0x1 struct]
And again: it has an identifier-- but how do I catch it?
So...what's the trick?
  댓글 수: 1
Bob Clark
Bob Clark 2018년 10월 11일
So then I tried:
classdef zippy
% ZIPPY anon
properties
spam
end % properties
methods
function obj = zippy(varargin)
spam = grumpy;
end % constructor
end % classdef
>> ehhi = zippy
Error using class
The name 'grumpy' is not a valid class name.
Error in newfangled.zippy (line <hash>)
obj = class(obj,'grumpy');
But 'grumpy' is just a string. How can it be an invalid class name? What gives?

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

답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by