R
Robert Klemme
Hi all,
this occurred to me some days ago and I'd like to hear what others think.
Operator += can't be user defined but instead is extended to a combination
of "+" and assignment:
"x += y" becomes implicitely "x = x + y".
Now, wouldn't it be better to turn that around and have people define
operator += and automatically convert operator + like in:
"x + y" becomes implicitely "x.dup += y"
The reason I'm offering this is performance: "x = x + y" is slower than an
inplace manipulation (that += would do), which is often what's desired
here.
On the downside:
- This will likely break a lot of code already defined unless one would
allow to define "+" for a while (possibly with a warning).
- The change has subtle effects, i.e., people relying on "x += y" to
create a new instance and not changing the state of x might encounter
surprising effects.
- dup might or might not be the appropriate method to create a copy
depending on the class at hand. When defining operator +, we are currently
free in deciding what's the type of result of this operation. That
freedom would definitely go away.
What do others think? Did I overlook something?
Thx!
robert
this occurred to me some days ago and I'd like to hear what others think.
Operator += can't be user defined but instead is extended to a combination
of "+" and assignment:
"x += y" becomes implicitely "x = x + y".
Now, wouldn't it be better to turn that around and have people define
operator += and automatically convert operator + like in:
"x + y" becomes implicitely "x.dup += y"
The reason I'm offering this is performance: "x = x + y" is slower than an
inplace manipulation (that += would do), which is often what's desired
here.
On the downside:
- This will likely break a lot of code already defined unless one would
allow to define "+" for a while (possibly with a warning).
- The change has subtle effects, i.e., people relying on "x += y" to
create a new instance and not changing the state of x might encounter
surprising effects.
- dup might or might not be the appropriate method to create a copy
depending on the class at hand. When defining operator +, we are currently
free in deciding what's the type of result of this operation. That
freedom would definitely go away.
What do others think? Did I overlook something?
Thx!
robert