% Dog.m -------------------------------------------------
classdef Dog
methods
function varargout = subsref(obj, x)
varargout = subsref_overloaded(obj, x);
end
end
end
% subsref_overloaded.m ----------------------------------
function varargout = subsref_overloaded(obj, x)
[varargout{1:nargout}] = builtin('subsref', obj, x);
end
d=Dog()
, and d()
yields
Maximum recursion limit of 500 reached.
Error in Dog (line 2)
[varargout{1:nargout}] = builtin('subsref', obj, x);
Caused by:
Maximum recursion limit of 500 reached.
Why, and can it be fixed? It's strange since it copies code straight from implementation , and in debugger, builtin('subsref', obj, x)
called while in Dog.m
works as expected, but not while in subsref_overloaded.m
.
Context : I'm overloading subsref
like here , but that's verbose to reuse so I want to make something like subsref_overloaded(obj, x, fn)
and output fn(x)
.
builtin
cares about where it's called from for some reason - it works properly if called from within the class itself (thanks @CrissLuengo). The following is an imperfect workaround
classdef Dog
methods
function varargout = subsref(obj, x)
[varargout{1:nargout}] = subsref_overloaded(obj, x);
if ~isempty(varargout) && varargout{1} == "special token"
[varargout{1:nargout}] = builtin('subsref', obj, x);
end
end
end
end
function varargout = subsref_overloaded(obj, x)
if % your cond here
% your logic here
else
[varargout{1:nargout}] = "special token";
end
end
Better alternatives / explanations welcome.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.