Hi there. I have observed a weird issue:
I have a class that exposes a method which returns me an instance of another class.
classdef Factory methods (Static) function worker = getWorker(inputItems) fprintf('No. of input arguments: %d\n', nargin); worker = Worker(); end endendclassdef Worker methods (Static) function doSomething() disp('Hello you'); end endend
When I call it like follows, everything works perfectly fine and all arguments in the getWorker method are where I'd expect them:
>> worker = Factory.getWorker(1); worker.doSomething()No. of input arguments: 1Hello you
However, when I use that returned instance directly, without assigning it to a variable. The arguments don't end up in the method, but rather the argument list is used as an index, for the returned objects (which could be an array … and hence non-numeric inputs mess everything up even more…):
>> Factory.getWorker(1).doSomething()No. of input arguments: 0Hello you
And my actual usecase would be the following, where my getWorker method does not have / need any input arguments, but then I get a warning:
>> Factory.getWorker().doSomething()No. of input arguments: 0Warning: A value of class "Worker" was indexed with no subscripts specified. Currentlythe result of this operation is the indexed value itself, but in a future release, itwill be an error. Hello you
Does anyone has an input, how I can enforce that the arguments end up in the right place, without assigning a real instance?
Used versions: Matlab 2017a and b on a Windows10 machine.
Best Answer