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

 
 

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

Submitted by:  dkeck <dkeck>
Submitted on:  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

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

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

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

Mike Miller <mtmiller>
Project Administrator
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.

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>
Project 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:

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

For more please see reference [2]

[1]

[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:
   
   
Comment:
   

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)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project 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.3