help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Some OOP questions


From: Judd Storrs
Subject: Re: Some OOP questions
Date: Thu, 9 Feb 2012 16:10:23 -0500

On Thu, Feb 9, 2012 at 3:45 PM, CdeMills <address@hidden> wrote:
>
> Judd Storrs-2 wrote
>>
>>
>> @wrapper/foo.m:
>> function w = foo(w, varargin)
>>     w.data = foo(w.data, varargin{:});
>> endfunction
>>
>> Is there a different way to handle that? Maybe something like a
>> generic "missing method" handler for the class?
>>
>>
>
> In the case of dataframes, I solved the problem like this
>
> @wrapper/foo.m:
> function resu = foo(A, B, varargin)
> try
>  resu = wrapper_func(@plus, A, B, varargin{:});
>  catch
>    disp(lasterr());
>    error("Operator foo problem for %s vs. %s", class(A), class(B));
>  end_try_catch
> end
>
> @wrapper/private/wrapper_func
> function resu = wrapper_func(func, A, B, varargin{:})
> if (isa(A, 'wrapper'))
>  if isa(B, 'wrapper'))
>     resu = feval(func, A.data, B.data, varargin{:});
>  else
>    resu = feval(func, A. data, B, varargin{:});
>
> and so on. This way, only private/wrapper_func has to care if arguments are
> or not of class wrapper. All overloaded functions only differ in the name
> they're transmitting to wrapper_func. Some genericity should be good, but
> it's doable without it.

I think maybe I wasn't clear. I was wondering if there's a way to
avoid creating:

@wrapper/sin.m
@wrapper/cos.m
@wrapper/permute.m
@wrapper/fft.m
@wrapper/ifft.m
@wrapper/abs.m
@wrapper/arg.m
@wrapper/imshow.m
@wrapper/imwrite.m
.
.
.

which is quite tedious because the only thing that varies is the
method name. So, I was wondering if there was a way to define a
wildcard method that would be called if no methods were defined.
The interpreter is going to toss an error and stop anyway.

Writing wrapper methods is just to prototype and simulate what it
would be like to use a subclass of a builtin type. Directly
subclassing the builtin would be preferable--it would avoid
interpreter overhead during dispatch.


--judd


reply via email to

[Prev in Thread] Current Thread [Next in Thread]