octave-bug-tracker
[Top][All Lists]
Advanced

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

[Octave-bug-tracker] [bug #42773] Operator Overloading of built-in class


From: Rik
Subject: [Octave-bug-tracker] [bug #42773] Operator Overloading of built-in classes not working as expected
Date: Wed, 16 Jul 2014 15:38:22 +0000
User-agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0

Update of bug #42773 (project octave):

                Category:                    None => Interpreter            
              Item Group:                    None => Matlab Compatibility   
                  Status:                    None => Postponed              
                 Summary: Operator Overloading not working as expected =>
Operator Overloading of built-in classes not working as expected

    _______________________________________________________

Follow-up Comment #1:

In object-oriented programming there are two approaches to accomplish what you
are trying to do.  In approach 1, you take the base class and tweak one of
it's methods through overloading.  In approach 2 you inherit from a base class
and extend the functionality of the base class with whatever behavior you
want.

For core objects, Matlab and Octave are slightly different.  Apparently Matlab
treats its core objects as just another class and allows overloading.  The
same strategy does not work for Octave.  I've changed the bug report Item
Group to Matlab Compatibility to reflect that.  But this is so deep in the
internals of Octave that I don't see it being changed for years, if ever.

On the other hand, you can get the same behavior (a new definition for '+'
which handles cell arrays) through approach 2.

In order to do that you just need a new class with a basic constructor that
wraps a cell array and a new function 'plus' that implements your extended
behavior.

I've attached an example class called @myclass.  If you untar the directory
somewhere and then start Octave in the directory above @myclass you should be
able to see how it works.

Here is a sample running the example you posted.


octave:4> A = mycell ([1 2 3], [4 5], 6);
octave:5> B = mycell (5, [4 5], 2);
octave:6> A + B

{
  [1,1] =

     6   7   8

  [1,2] =

      8   10

  [1,3] =  8
}


Note that I did this quickly as a proof of concept.  If you really go this
direction then you should use a better class name and maybe put in some checks
on what the inputs are, etc.


    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?42773>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

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