How are method(obj) calls handled in MATLAB?

Now, as far as I can tell, calling obj.method or method(obj) seems to give the same results. However, when I tried to see how subsref behaves in these cases, I came to the startling realization that method(obj) doesn't seem to go through the subsref mechanism. So how are these calls then handled and is there a practical way to override them?
I'm trying to do this, so I can make something like method(object).otherMethod possible (which I would obviously be handling through a custom subsref). Any ideas?
(I also asked about this on StackOverflow)

回答 (2 件)

Sean de Wolski
Sean de Wolski 2013 年 3 月 5 日

0 投票

I would guess that it uses the metaclass function or ? equivalent.
obj = MyClass;
mc = metaclass(obj);
mc.MethodList
Equivalent to:
mc = ?MyClass;
mc.MethodList;

3 件のコメント

Vladimir Peri
Vladimir Peri 2013 年 3 月 6 日
Huh, not sure I follow what you mean here. In any case, I guess this means it's not possible to overload this behavior?
Sean de Wolski
Sean de Wolski 2013 年 3 月 6 日
I completely misunderstood what you were asking. I thought you were asking how:
methods(obj)
worked. There is a thread somewhere on the difference between obj.foo and foo(obj). I'll see if I can find it.
Walter Roberson
Walter Roberson 2013 年 3 月 6 日
There was a recent thread that indicated that obj.method bypassed some checks that method(obj) would otherwise use.

サインインしてコメントする。

Matt J
Matt J 2013 年 3 月 6 日

0 投票

method(obj) is handled like any other function call. To enable the kind of syntax you're looking for, you could do something very similar to this
but I doubt it's worth it.
Incidentally, obj.method does not (always) go through subsref either. Even if your class defines a subsref method, when you invoke obj.method from within the classdef, subsref is not called. Inside the classdef, obj.method is entirely equivalent to method(obj).

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

タグ

質問済み:

2013 年 3 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by