Creating a function handle to an overloaded `end` function
Asked Answered
W

1

10

MATLAB allows overloading various operators for custom classes. One of the unlisted overloadable operators is end, as can be learned from \matlab\lang\end.m:

%   END(A,K,N) is called for indexing expressions involving the object A
%   when END is part of the K-th index out of N indices.  For example,
%   the expression A(end-1,:) calls A's END method with END(A,1,2).

An example of such a method is table.end (paste in the MATLAB command line and press "Open Selection" to go to its definition; it is defined in ...\matlab\datatypes\@tabular\end.m).

Unlike a normal method, one cannot simply write hEnd = @end, because this gives the error:

>> hEnd = @end;
 hEnd = @end;
         ↑
Error: Illegal use of reserved keyword "end".

On the other hand, writing e = str2func('end'); works, but it links to the default end function (even when temporarily switching to the folder where the desired end.m is found).

Failed attempts include str2func('table>end');, str2func('table\end');, str2func('table.end'); and @(a,b,c)table.end(a,b,c);.

My question: How do I create a handle to the end function of a specific class?

Westerman answered 31/3, 2017 at 6:42 Comment(13)
If it's a static method of a class, I expect @()className.end should work.Overweight
@Overweight Here's what happens: e = @(a,b,c)table.end(a,b,c); e(1,2,3) -> The class table has no Constant property or Static method named 'end'. At least on my R2017a...Westerman
Ah, I see now. While it's a method listed in metaClass.MethodList (metaClass=?table), it's not part of the class definition and thus not available via dot-reference. Hm...Overweight
what about: hEnd = @(A,K,N) feval('end',A,K,N);?Peplum
@Peplum Why would that work? This would likely call the end function of class A - but that's the thing - I don't want that end, but some B's end. Whether B's function is applicable to A is something I (the user) will worry about later.Westerman
This is not how function handles work. You simply cannot call a function overloaded for type A without an input argument of type A. You cannot define a function handle to a specific overload. You can define a function handle to end, and MATLAB decides which end to call depending on the input arguments (as in the answer by Daren Shan). This has nothing to do with anything specific about end. Try to do this with size, you'll get the same errors.Barajas
The question here is: why do you want to do this? If it is about not implementing this method for your own class, you should probably just make a soft link into your own class directory. But better is just copy-paste the code. That end method is absolutely trivial.Barajas
@CrisLuengo - Why do I want this? Because I am exploring the limits of MCOS (the applicability of something like "reflection"). The concept of "Method References" exists in other languages. "You simply cannot call a function overloaded for type A without an input argument of type A" - the falsehood of this assertion is easy to demonstrate using static methods. Since MATLAB doesn't enforce type safety, there's nothing stopping you from calling such methods with whichever arguments - and this might even work!Westerman
That is true, static methods do not require an object of the class as input. They don't require inputs at all. So this would work with static methods. I've been using MATLAB since the 90's, static methods are kinda new to me... :) But I an sure that this cannot be used with "regular" methods, the functions that you stick in a @xxx directory so that they are specific to that type (and by extension, also those defined in classdef files). And I'm glad it's not possible, you could break a lot of my code if it were!Barajas
Do you have a link for MCOS? Google doesn't know what it is (or rather, there are at least 10 other things that it thinks are more important). I'm curious to learn about it!Barajas
By the way, a static method is not an overloaded method.Barajas
@CrisLuengo MCOS stands for "MATLAB class object system", I believe I encountered this terminology on undocumentedmatlab.com.Westerman
Perhaps some of the confusion stems from a problem with the question, since I should've written "override" (as in redefine in a subclass) and not "overload" (as in having several methods with the same name only differing by inputs/outputs; which is not supported in MATLAB, although there are ways around it using varargin, nargin, etc.).Westerman
C
1

Overloading — If the function you specify overloads a function in a class that is not a fundamental MATLAB class, the function is not associated with the function handle at the time it is constructed. Instead, MATLAB considers the input arguments and determines which implementation to call at the time of evaluation.


Function handles store their absolute path, so when you have a valid handle, you can invoke the function from any location. You do not have to specify the path to the function when creating the handle, only the function name.


so if your 'end' function is in matlab path , matlab consider it as a candidate for evaluation depending on the inputs,in your case if input object is of 'table' class type the feval(str2func('end'),i,j) evaluate the end function which is defined in the folder @table/end.m

Circumspection answered 31/3, 2017 at 11:47 Comment(2)
This missed the point of the question. I didn't ask how MATLAB decides which overloaded method to call, nor did I say I had a table object for MATLAB to resolve the correct end function. What I have is some other class (or structure), which is modeled after table (but does not inherit from it), such that it could make use of table's end - if only I could link to that function somehow... Arguably, if we were talking about a private instance method, my question would be invalid, but here we are just talking about a handle to a function defined in an .m file with a special name...Westerman
I don't think this answer misses the point of the question. You cannot get the handle to specific overloads. You get the handle to the end function, and MATLAB decides which one to call depending on the input arguments.Barajas

© 2022 - 2024 — McMap. All rights reserved.