Alternate Constructor Behavior with inputParser

10 views (last 30 days)
Hi,
I have several classes whose constructors rely on InputParser to collect all the relevant properties. I feed all of the varargin array to the inputParser object. Then I loop over the results struct of the inputParser object and set each field of my class instance equal to the value of the matching field of the struct.
parser.parse(varargin{:});
results = parser.Results;
field_list = fields(results);
for field_index = 1:length(field_list)
field = field_list{field_index};
self.(field) = results.(field);
end
While this works well and allows me to avoid complex syntax in the constructor (not to mention implementation logic), I don't have a good way to emulate multiple-constructor behavior. As I understand it, the standard way to have a non-default constructor is to switch on nargin inside the main constructor. With the inputParser approach, though, that's not reliable (as some inputs are optional, I can't predict nargin with any certainty). I could probably add an optional argument(s), but it's still awkward.
My typical use case is that I need to build an object directly from a string or input file with a syntax defined externally. While in other languages I'd probably use a secondary constructor that takes in either the file path or a string, in MATLAB the best I've come up with is to have a separate function called load_whatever.m that takes in the non-default constructor arguments and calls the default (only) constructor in the usual way. (This is slightly reminiscent of the Factory Design Pattern, but I don't know if the inventors of that one would approve.) Unfortunately, this involves moving class-specific code outside of the class definition file, and is neither object-oriented nor easily configurable.
The only other thing I can think of would be to have a static class method that could act as a secondary constructor. I have a feeling that's a bad idea on more than one count, so I haven't tried that yet.
What's the preferred practice for doing things in single-constructor languages like MATLAB that in multiple-constructor languages would use a second constructor? Is the thing I want to do here even one of those things?
Thanks, Jay
  1 Comment
Jiro Doke
Jiro Doke on 6 Apr 2011
Can you elaborate on what type constructor signature you want to have? Is the example below your actual case, i.e. two input constructor and a structure input constructor?
I'm not promising anything, but maybe if we have an idea of what exact constructor behaviors you're looking for, we can try to figure it out.

Sign in to comment.

Accepted Answer

Jay
Jay on 6 Apr 2011
So far, the static method is a leading candidate. Ruby has the same problem http://www.ruby-forum.com/topic/160572#706772
%%classA.m
classdef classA
properties(Access = public)
foo
bar
end
methods(Access = public)
function self = classA(f, b)
self.foo = f;
self.bar = b;
end
end
methods(Access = public, Static)
function result = alt_constructor(some_structure)
result = classA(some_structure.foo_field,some_structure.bar_field);
end
end
end
%%test_classA.m
a = classA(1, 0)
b = classA.alt_constructor(struct('foo_field', 2, 'bar_field', 99))
>> test_classA
a =
classA
Properties:
foo: 1
bar: 0
Methods
b =
classA
Properties:
foo: 2
bar: 99
Methods
  2 Comments
Jiro Doke
Jiro Doke on 6 Apr 2011
I'm kind of confused. Typically, if you want to allow an equivalent structure input, then I feel that the corresponding non-structure syntax should be a param-value pair. For example, one might use this syntax:
a = classA('foo', 1, 'bar', 0)
or this syntax:
b = classA(struct('foo', 2, 'bar', 0))
In this case, inputParser would take care of it with the "addParamValue" method.
Jay
Jay on 7 Apr 2011
Right, that example doesn't actually cover the first half of my rant. In most actual classes, I do call the primary constructor that way, or with a structure input. All this code does is demonstrate that it's possible for a static method to call a constructor. For some reason I was thinking that wouldn't work.

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!