bugGNU Octave - Bugs: bug #42773, Operator Overloading of built-in...


bug #42773: Operator Overloading of built-in classes not working as expected

Submitter:  dkeck <dkeck>
Submitted:  Tue 15 Jul 2014 10:06:09 AM UTC
Category:  Interpreter Severity:  2 - Minor
Priority:  1 - Later Item Group:  Matlab Compatibility
Status:  Postponed Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup

Fri 27 Apr 2018 06:19:09 PM UTC, comment #2: 

Well this does partially work, if the method '@cell/plus.m' is made available on the Octave load path, then

>> plus (A, B)
ans =
  [1,1] =

     6   7   8

  [1,2] =

      8   10

  [1,3] =  8

does work. The only thing missing is tying the operator syntax itself to a method lookup.

Mike Miller <mtmiller>
Group Member
Wed 16 Jul 2014 03:38:22 PM UTC, 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.

Rik <rik5>
Group administrator
Tue 15 Jul 2014 10:06:09 AM UTC, original submission:  

I put the plus.m [1] file into a folder called @cell under Octave root (/octave-3.8.0/@cell/plus.m) and added the @cell folder to the path.

Create two cell arrays and try to add them with '+' sign for testing:

A = {[1 2 3] [4 5] 6};
B = {5 [4 5] 2};
error: binary operator '+' not implemented for 'cell' by 'cell' operations

As you can see the mapping of the operator to plus.m fails.

For more please see reference [2]


function C = plus(A,B)
  C = cellfun(@plus,A,B,'UniformOutput',false);

[2] http://octave.1599824.n4.nabble.com/Operator-Overloading-not-working-as-expected-td4665479.html

dkeck <dkeck>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Attached Files
file #31729:  @mycell.tgz added by rik5 (420B - application/x-gtar)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by nul0m
  • -email is unavailable- added by dkeck (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.


    Follow 10 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-04-27 mtmiller Severity3 - Normal 2 - Minor
        Priority5 - Normal 1 - Later
        Release3.8.0 dev
        Operating SystemMicrosoft Windows Any
    2014-07-16 rik5 Attached File- Added @mycell.tgz, #31729
    2014-07-16 rik5 CategoryNone Interpreter
        Item GroupNone Matlab Compatibility
        StatusNone Postponed
        SummaryOperator Overloading not working as expected Operator Overloading of built-in classes not working as expected
    2014-07-15 nul0m Carbon-Copy- Added -email is unavailable-

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code