Enable memberwise operators for tuples #5154
Replies: 4 comments 6 replies
-
Thinking point:
|
Beta Was this translation helpful? Give feedback.
-
It's not a perfect solution, but you can use the .NET6 preview: |
Beta Was this translation helpful? Give feedback.
-
Though not that conveniently, but you can already do that with Otherwise, you can create extension methods as suggested by @FaustVX. It's definitely a better approach than providing some some built-in operator set that would just throw |
Beta Was this translation helpful? Give feedback.
-
The only lacked thing is extension interface implementation so you can implement |
Beta Was this translation helpful? Give feedback.
-
Summary
Memberwise tulpe operators refer to applying the operator to respective members of same-sized tuples. For example:
These operators will apply to all member types that support the given operators. Tuples whose elements bear different types are also supported, for long as their respective elements support the given operator. In other words, this feature is supported under the given constraints:
Given the tuples t1, t2,
t1.ItemCount == t2.ItemCount
(made-up property; tuple item counts must be equal)t1[i] <op> t2[i]
must be supported, where t1[i] refers to the i-th item of the tupleExamples
t1 + t2 is supported because:
int
, t2[0] isstring
, andint + string
is a valid operatort1 - t2 is not supported because for the items at index 2,
string - int
is not a valid operator.Consequences
This would also enable compound assignment operators for all the newly supported memberwise tuple operators.
Motivation
Some operations involve memberwise operations on tuples, where respective elements are performed the same operation, yielding the desired result. Be it adding 2D vectors, dividing all by a scalar, or XORing all values by a mask.
Drawbacks
Beta Was this translation helpful? Give feedback.
All reactions