How to call a class-specified overloaded version of a function without using an object instance as an argument
1 view (last 30 days)
I have a class:
classdef fast_calc < handle
function obj = fast_calc(x)
obj.value = x;
function y = sin(obj)
x = obj.value;
x = obj;
% Fast calculation of sin using a Taylor expansion:
y = x - x.^3 / 6;
the function sin works fine on a fast_calc object:
>> myNum = fast_calc(pi/6);
However, I would like also to be to call it directly on a standard numeric variable, something like:
but this returns an error
The class fast_calc has no Constant property or Static method named 'sin'.
I don't want to define 'sin' as a static method because then sin(myNum) will not automatically invoke the overloaded function.
Is there a solution which allows calling an overloaded method both automatically on an instance, and on other object types when explicitly requesitng it?
per isakson on 21 Aug 2021
Edited: per isakson on 21 Aug 2021
"I don't want to define 'sin' as a static method because then sin(myNum) will not automatically invoke the overloaded function"
I have defined sin() as a Static method (attached) and here (R2018b) both ways of calling works
fc = fast_calc(pi/6);
fast_calc.sin( fc )
Ok, that's not what you asked, but AFAIK it's what Matlab offers.