[Proposal] Assigning one value to all items in a tuple #1725
Replies: 9 comments
-
public MyType() => IsFoo = HasBar = ShouldBuzz = true; |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Wow, can't believe I forgot that, I use that pretty frequently too. Talk about overthinking it 😂 |
Beta Was this translation helpful? Give feedback.
-
Actually, I realized there are times where it would be more uniquely useful, and that's assigning to multiple values. (X, Y, Z) = forceCube ? (GetSizeForX(), GetSizeForY(), GetSizeForZ()) : (GetSizeForX(), GetSizeForX(), GetSizeForX())
(X, Y, Z) = forceCube ? (GetSizeForX(), GetSizeForY(), GetSizeForZ()) : GetSizeForX(); The first line doesn't even fit on this page without having to be scrolled. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure it is. Based on the names of the methods you used, I would probably expect that if the condition is |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
The bottom line gives me the impression that it's trying to smooth over something inherently arbitrary and make it look fundamental. |
Beta Was this translation helpful? Give feedback.
-
We don't need language changes to fix that.
Regardless of the formatting, if I saw that in a code review I'd flag it for modification - using the ternary operator obfuscates the fact that X is always assigned the same thing. (I'd also observe that the sense of To me, this is much clearer:
|
Beta Was this translation helpful? Give feedback.
-
Except you'd be calling But then you could do this: X = GetSizeForX();
Y = forceCube ? X : GetSizeForY();
Z = forceCube ? X : GetSizeForZ(); |
Beta Was this translation helpful? Give feedback.
-
public static (T,T) RepeatTuple2<T>(this T obj) => (obj,obj);
public static (T,T,T) RepeatTuple3<T>(this T obj) => (obj,obj,obj);
public static (T,T,T,T) RepeatTuple4<T>(this T obj) => (obj,obj,obj,obj);
////
(X, Y, Z) = forceCube ? (GetSizeForX(), GetSizeForY(), GetSizeForZ()) : GetSizeForX().RepeatTuple3(); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem:
It feels sort've redundant to do something like
Where each item is getting the same value.
Purposed Solution:
So what if there was a way we could write something like this:
And
true
will be assigned to all three variables. This makes the code shorter, easier to read, and if I decide later I want them to all start off as false, I change it in only one place.Questions:
If someone did
Do they all have their own
List<int>()
(meaning the right hand of the assignment gets executed once for each item in the tuple) or they all share the same reference to that one list? (meaning the right hand gets executed only once in total). I think it would be more useful if they each got their ownList<int>
so that way we could quickly and easily initialize multiple variables of the same time.EDIT: Made better example
Beta Was this translation helpful? Give feedback.
All reactions