You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow user types to customize behavior of compound assignment operators in a way that the target of the assignment is
modified in-place.
Motivation
C# provides support for the developer overloading operator implementations for user-defined type.
Additionally, it provides support for "compound assignment operators" which allow the user to write
code similarly to x += y rather than x = x + y. However, the language does not currently allow
for the developer to overload these compound assignment operators and while the default behavior
does the right thing, especially as it pertains to immutable value types, it is not always
"optimal".
with the current language rules, compound assignment operator c1 += 1 invokes user defined + operator
and then assigns its return value to the local variable c1. Note that operator implementation must allocate
and return a new instance of C1, while, from the consumer's perspective, an in-place change to the original
instance of C1 instead would work as good (it is not used after the assignment), with an additional benefit of
avoiding an extra allocation.
When a program utilizes a compound assignment operation, the most common effect is that the original value is
"lost" and is no longer available to the program. With types which have large data (such as BigInteger, Tensors, etc.)
the cost of producing a net new destination, iterating, and copying the memory tends to be fairly expensive.
An in-place mutation would allow skipping this expense in many cases, which can provide significant improvements
to such scenarios.
Therefore, it may be beneficial for C# to allow user types to
customize behavior of compound assignment operators and optimize scenarios that would otherwise need to allocate
and copy.
User Defined Compound Assignment Operators
Summary
Allow user types to customize behavior of compound assignment operators in a way that the target of the assignment is
modified in-place.
Motivation
C# provides support for the developer overloading operator implementations for user-defined type.
Additionally, it provides support for "compound assignment operators" which allow the user to write
code similarly to
x += y
rather thanx = x + y
. However, the language does not currently allowfor the developer to overload these compound assignment operators and while the default behavior
does the right thing, especially as it pertains to immutable value types, it is not always
"optimal".
Given the following example
with the current language rules, compound assignment operator
c1 += 1
invokes user defined+
operatorand then assigns its return value to the local variable
c1
. Note that operator implementation must allocateand return a new instance of
C1
, while, from the consumer's perspective, an in-place change to the originalinstance of
C1
instead would work as good (it is not used after the assignment), with an additional benefit ofavoiding an extra allocation.
When a program utilizes a compound assignment operation, the most common effect is that the original value is
"lost" and is no longer available to the program. With types which have large data (such as BigInteger, Tensors, etc.)
the cost of producing a net new destination, iterating, and copying the memory tends to be fairly expensive.
An in-place mutation would allow skipping this expense in many cases, which can provide significant improvements
to such scenarios.
Therefore, it may be beneficial for C# to allow user types to
customize behavior of compound assignment operators and optimize scenarios that would otherwise need to allocate
and copy.
Design meetings
The text was updated successfully, but these errors were encountered: