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

4 visualizzazioni (ultimi 30 giorni)
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 Commento
Bob Clark
Bob Clark il 11 Ott 2018
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?

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Methods in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by