Is it possible to dynamically add methods to an object, or to build a generic method that "catches attempts to access nonexistent methods"?
조회 수: 26 (최근 30일)
이전 댓글 표시
Dear all,
I have seen similar questions asked serveral times, e.g. here @mathworks and here @stackoverflow, but it never really applies to my context.
Hence my question: is it possible to do one of the following operations (?)
- Add methods to an object dynamically during instantiation (from the constructor).
- "Catch attempts to access a nonexistent method" by overloading some method (the way we overload subsasgn()/subsref() for managing indexing).
- Subclass a given class dynamically, during instantiation (from the constructor). [I am a little less interested in this one, because we cannot subclass all built-ins].
The reason is the following. I built a class that allows users to "extend" existing objects passed to the constructor. To illustrate
>> a = 2 ; % double
>> b = MyClass( a ) ; % double extended
Now I would like to "delegate" whatever method call to double if it is not implemented in MyClass. I can easily manage two situations, that I illustrate with a sin() function/method:
- I implement sin() in MyClass, so any sin(b) or b.sin() calls MyClass.sin() which can manage to apply sin() to the double 2. This is not a practicable solution in my case, because I cannot implement all possible methods for all possible "classes-to-extend".
- Without implementing MyClass.sin(), I can catch such calls with subsref() and manage the rest. This is again not a working solution, because sin(b) is not caught by subsref(). It seems actually that MATLAB tests whether MyClass/b has a sin() method when we evaluate sin(b) and returns an error if not, instead of trying the call and failing.. (?)
So is there a way to catch sin(b) with a subsref()-like method/overload, or a way to have MATLAB calling a specific method of MyClass/b when calls to nonexistent methods (or tests of existence) are attempted?
Thank you and best regards,
Cedric
댓글 수: 0
채택된 답변
추가 답변 (2개)
Daniel Shub
2013년 1월 17일
Probably not optimal, but you might be able to dynamically overload what classname returns, which in turn may affect what is called. I tried this in this answer:
There is also an answer by Malcolm that might be helpful.
댓글 수: 3
Paul Wintz
2021년 10월 4일
You can make
x = MyClass(2);
cos(x);
work by adding the following method to MyClass:
classdef MyClass
methods
function y = cos(x)
y = builtin('cos', x);
end
end
end
Darik
2013년 1월 17일
It seems to work (in 2012b, at least) if you subclass dynamicprops
%
classdef MyClass < dynamicprops
properties
Data
end
methods
function self = MyClass (x)
self = self@dynamicprops;
self.Data = x;
end
function varargout = subsref (self, S)
try
[varargout{1:nargout}] = builtin('subsref', self, S);
catch
[varargout{1:nargout}] = feval(S(1).subs, self.Data);
end
end
end
end
>> a = MyClass(0);
>> a.Data %normal property access
ans = 0
>> a.my_method() %normal method call
ans = 100
>> a.cos() %overridden method call
ans = 1
참고 항목
카테고리
Help Center 및 File Exchange에서 Methods에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!