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
properties
spam(5,1) grumpy
end
end
>> 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
end
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
properties
spam
end
methods
function obj = zippy(varargin)
spam = grumpy;
end
end
end
>> 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
properties
spam @(ham) grumpy (ham)
end
end
>> 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
properties
spam function_handle
end
end
>> 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?