Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 17, 2002 Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
I'm looking at the overloading of commutative operators add, multiply, etc. There are a couple of options for dealing with the (1 + a) case: 1) Provide a separate "add_r" function to make a.add_r(1) 2) Allow the compiler to swap the operands and call a.add(1) 3) Allow the compiler to swap the operands unless there is an "add_r" function defined. I'm leaning towards (2), it is simpler and in keeping with the way the builtin operators work. Note that this would only apply to those operators that are mathematically commutative. It does not allow associative rearrangement, and does not allow replacing (a - b) with (a + (-b)), etc. |
August 17, 2002 Re: Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote:
> I'm looking at the overloading of commutative operators add, multiply, etc.
> There are a couple of options for dealing with the (1 + a) case:
>
> 1) Provide a separate "add_r" function to make a.add_r(1)
> 2) Allow the compiler to swap the operands and call a.add(1)
> 3) Allow the compiler to swap the operands unless there is an "add_r"
> function defined.
>
> I'm leaning towards (2), it is simpler and in keeping with the way the
> builtin operators work. Note that this would only apply to those operators
> that are mathematically commutative. It does not allow associative
> rearrangement, and does not allow replacing (a - b) with (a + (-b)), etc.
It's not really worth the gain when you can write out a basic reverse with one line:
Vector operator (extended a) + (Vector b) { return b + a; }
Vector operator (extended a) - (Vector b) { return -b + a; }
Vector operator (extended a) * (Vector b) { return b * a; }
IMO it's clearer to have every permutation written out and doesn't interfere with getting anything done. It's not worth the special case effort for the two operators which can sometimes sorta be automatically reversed.
|
August 17, 2002 Re: Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Fri, 16 Aug 2002 17:54:54 -0700 "Walter" <walter@digitalmars.com> wrote:
> I'm leaning towards (2), it is simpler and in keeping with the way the
> builtin operators work. Note that this would only apply to those operators
> that are mathematically commutative. It does not allow associative
> rearrangement, and does not allow replacing (a - b) with (a + (-b)), etc.
And are there any operators that are _always_ commutative? I might be wrong,
but I remember some kind of numbers in math for which + is not commutative...
IMHO it's better to define operators separately for every case.
|
August 17, 2002 Re: Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say either 1 or 3. Or 1 and 2 (providing a both operation). "Walter" <walter@digitalmars.com> wrote in message news:ajk6mq$2o08$1@digitaldaemon.com... > I'm looking at the overloading of commutative operators add, multiply, etc. > There are a couple of options for dealing with the (1 + a) case: > > 1) Provide a separate "add_r" function to make a.add_r(1) > 2) Allow the compiler to swap the operands and call a.add(1) > 3) Allow the compiler to swap the operands unless there is an "add_r" > function defined. > > I'm leaning towards (2), it is simpler and in keeping with the way the > builtin operators work. Note that this would only apply to those operators > that are mathematically commutative. It does not allow associative > rearrangement, and does not allow replacing (a - b) with (a + (-b)), etc. > > |
August 17, 2002 Re: Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:CFN374854774504051@news.digitalmars.com... > And are there any operators that are _always_ commutative? I might be wrong, > but I remember some kind of numbers in math for which + is not commutative... I can't think of any. > IMHO it's better to define operators separately for every case. |
August 17, 2002 Re: Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to anderson | "anderson" <anderson@firestar.com.au> wrote in message news:ajkvq6$h3d$1@digitaldaemon.com... > A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say either 1 > or 3. Or 1 and 2 (providing a both operation). The only time the commutativity is an issue is when the left operand is not a class object, i.e. is a scalar or array. Isn't matrix math commutative when adding/multiplying by a constant? |
August 17, 2002 Re: Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | I agree with Pavel here, it should be standard across all operators. That way there's no confusion. "Walter" <walter@digitalmars.com> wrote in message news:ajl03j$hj4$1@digitaldaemon.com... > > "Pavel Minayev" <evilone@omen.ru> wrote in message news:CFN374854774504051@news.digitalmars.com... > > And are there any operators that are _always_ commutative? I might be > wrong, > > but I remember some kind of numbers in math for which + is not > commutative... > > I can't think of any. > > > IMHO it's better to define operators separately for every case. > > > > |
August 17, 2002 Re: Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:ajl2ei$106c$1@digitaldaemon.com... > > "anderson" <anderson@firestar.com.au> wrote in message news:ajkvq6$h3d$1@digitaldaemon.com... > > A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say either > 1 > > or 3. Or 1 and 2 (providing a both operation). > > The only time the commutativity is an issue is when the left operand is not > a class object, i.e. is a scalar or array. Isn't matrix math commutative when adding/multiplying by a constant? > Your right. I suppose if there are any cases for non commutative you could define an integer as a class and get around it that way. |
August 17, 2002 Re: Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Come to think of it.... How will... Matrix ** Integer be handled? "Walter" <walter@digitalmars.com> wrote in message news:ajl2ei$106c$1@digitaldaemon.com... > > "anderson" <anderson@firestar.com.au> wrote in message news:ajkvq6$h3d$1@digitaldaemon.com... > > A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say either > 1 > > or 3. Or 1 and 2 (providing a both operation). > > The only time the commutativity is an issue is when the left operand is not > a class object, i.e. is a scalar or array. Isn't matrix math commutative when adding/multiplying by a constant? > > |
August 17, 2002 Re: Reverse Operator Overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to anderson | "anderson" <anderson@firestar.com.au> wrote in message news:ajl71t$14of$1@digitaldaemon.com... > Come to think of it.... > > How will... > > Matrix ** Integer Integer ** Matrix > > be handled? > > > "Walter" <walter@digitalmars.com> wrote in message news:ajl2ei$106c$1@digitaldaemon.com... > > > > "anderson" <anderson@firestar.com.au> wrote in message news:ajkvq6$h3d$1@digitaldaemon.com... > > > A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say > either > > 1 > > > or 3. Or 1 and 2 (providing a both operation). > > > > The only time the commutativity is an issue is when the left operand is > not > > a class object, i.e. is a scalar or array. Isn't matrix math commutative when adding/multiplying by a constant? > > > > > > |
Copyright © 1999-2021 by the D Language Foundation