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