bugGNU Octave - Bugs: bug #51605, allow class overload of unary...

 
 

bug #51605: allow class overload of unary operators ++ and -- via function

Submitted by:  Ernst Reissner <ernstreissner>
Submitted on:  Fri 28 Jul 2017 01:02:15 PM UTC  
 
Category: InterpreterSeverity: 3 - Normal
Priority: 5 - NormalItem Group: Feature Request
Status: NoneAssigned to: None
Originator Name: Open/Closed: Open
Release: devOperating System: Any

Add a New Comment(Rich Markup)
   

You are not logged in

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

 

(Jump to the original submission Jump to the original submission)

Wed 23 Aug 2017 07:18:18 AM UTC, comment #9:

The -- and ++ operators are implemented by the op_decr and op_incr non_const_unary_op types. Search for non_const_unary_op in libinterp/parse-tree to see where it is called, and search for NCUNOP in libinterp/operators to see where corresponding operators are installed for each numeric type.

Mike Miller <mtmiller>
Project Administrator
Tue 22 Aug 2017 09:48:40 PM UTC, comment #8:

@mike,
Thank you for doing some research.
Could you point me to the code for a+=b
and for ++a and for a++?
Then I'll try to fix that.

Ernst Reissner <ernstreissner>
Mon 14 Aug 2017 04:40:55 PM UTC, comment #7:

Ernst - I've looked at this a little bit, and the problem may lie in the way the operators are defined in the parser. When the parser encounters an assignment operator like +=, then it is explicitly doing an assignment expression with a left operand and a right operand. But when the parser encounters a ++ or -- unary operator, it does not expand this into a full assignment expression. It is instead treated as a modify-in-place-unary-operator expression, and the increment or decrement operator is called on the single object. The object is expected to be responsible for modifying itself in place when the "decr" or "incr" operator is called on it.

You are welcome to try to implement these operator overloads if you want, and see how it will work and what the consequences will be.

Mike Miller <mtmiller>
Project Administrator
Mon 07 Aug 2017 08:55:11 AM UTC, comment #6:

@John
maybe I do not understand correctly.

Example: I have a class pn (polymorphic numbers)
whatever it is, it comprises double numbers.

I can write
a = pn(4);
a += pn(3);
and then a is 7 (as pn-number)

I just overwrote plus(a,b)
which overwrites not only + as in matlab, but also +=.
So far so good.
It is, as you say, syntactic sugar,
i.e. a+=b is short for a = a + b.
Thus I think it is mandatory,
that plus overwrites not only + but also +=.

Now i think, that ++a is just short for a+=1.
Consequently, i think plus shall affect also ++a operator.
The result should be something like
a+=1, where 1 must be the one-element in the class.
I think ++a should be short for a+=pn(1),
if a has type pn,
i.e. it should be short for a = plus(a,pn(1)).
To use ++, pn(1) must be defined of course.

Ernst Reissner <ernstreissner>
Fri 04 Aug 2017 11:00:41 PM UTC, comment #5:

Since function arguments are passed as if by value, how would overloading ++ and -- work? Are you proposing that you could only overload them for handle classes?

It seems to me that it would be far simpler to just say that ++ and -- are special syntax that only applies to built-in numeric types and that they can't be overloaded.

Similarly, I think the OP= operators should just be syntactic sugar for RHS = RHS OP LHS.

John W. Eaton <jwe>
Project Administrator
Fri 04 Aug 2017 01:13:35 PM UTC, comment #4:

Yes, i would like to do the implementation.
The usefulness can also be explained:

in matlab, oo allows to overwrite most operations.
Among those also +, -, *, /.
The result is, that these can be used with objects also.

This works as follows:
To these operation are tied functions
plus, minus, mtimes, mrdivide.
Overwriting these functions
results in overwriting the according operations.
So a+b is just a shortcut for plus(a, b).

Unlike Matlab, Octave provides besides the assignment operation =
also the shortcuts +=, -=, *= and /=.
As you know, a += b is just a shortcut for a = a + b
and this in turn for a = plus(a,b).
So to use += in the oo context, i.e. with objects,
one can write a += b only, if this has the meaning of
a = plus(a,b).

Also unlike Matlab, Octave has operator ++,
both prefix and postfix.
++a is just a shortcut for a += 1.
So ++a means nothing but a = plus(a, 1).

But what is 1 in the oo-context? i suggest just cln(1),
where cln is the name of the class and thus of the constructor.
If this is not defined, then adding 1 does not make sense
and I think adding in general does not.

So So ++a means nothing but a = plus(a, cln(1)).

Think of using this in the context of a polynomial
or of defining another kind of arithmetics in octave
like rational arithmetics,
interval arithmetics,
continued fractions or something else
(which is actually what i do).

Ernst Reissner <ernstreissner>
Fri 04 Aug 2017 10:28:23 AM UTC, comment #3:

No need to get discouraged. Octave developers are always
open to helpful suggestions. Are you interested in doing the
implementation? Or making a more detailed argument for
the usefulness of this change?

Michael Godfrey <godfrey>
Project Member
Fri 04 Aug 2017 10:02:18 AM UTC, comment #2:

I see: feature requests are ignored so please close this for me.

Ernst Reissner <ernstreissner>
Mon 31 Jul 2017 11:00:24 PM UTC, comment #1:

I would be willing to fix this, if someone agrees, that this is really a valuable feature request.
I think, rarely anyone uses oo and so maybe noone is aware of the gap.

Ernst Reissner <ernstreissner>
Fri 28 Jul 2017 01:02:15 PM UTC, original submission:

In contrast to matlab,
octave provides operators += and ++ (the latter prefix and postfix).
Both are based on the operator + which is in turn defined by the function plus(x,y).

in oo-programming, one can overwrite operators like +
by just overwriting the associated function like plus.
Whereas this automatically overwrites +=, it does not overwrite ++.
I think this is inconsistent.

Ernst Reissner <ernstreissner>

 

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

Attach File(s):
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by mtmiller (Posted a comment)
  • -unavailable- added by jwe (Posted a comment)
  • -unavailable- added by godfrey (Posted a comment)
  • -unavailable- added by ernstreissner (Submitted the item)
  • -unavailable- added by ernstreissner
  •  

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

    Only project members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 2 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Mon 14 Aug 2017 04:40:55 PM UTCmtmillerSummaryoperands ++ and -- via function=>allow class overload of unary operators ++ and -- via function
    Fri 28 Jul 2017 01:02:15 PM UTCernstreissnerCarbon-Copy-=>Added -unavailable-

    Back to the top


    Powered by Savane 3.1-cleanup1