Support value tuples with single value using an extra comma in the list (example var tuple = (int x,)
)
#4668
Replies: 5 comments 20 replies
-
Dupe of: #1595 |
Beta Was this translation helpful? Give feedback.
-
Please don't add awful syntax just because |
Beta Was this translation helpful? Give feedback.
-
I was needing this today. My personal take is I wanted a named single value tuple so that I can easily expand the return type of a method in the future when/if I need to return additional data points. For example:
So, traditionally, with the first method, to explicitly imply that I am returning a method with ID's, I would typically name it something like "GetItemIDs", however if my future intent is to also return other properties of Item, then I'd have to create a whole new method, or modify the function and update every consumer. What I would like to do is specify upfront that I am returning a complex type, that at least has an ID property, which can easily be appended to down-the-road w/o breaking any downstream consumers (assuming I don't change type/property name). Of course, by default I could just always return a struct/class, however the point here is sometimes you just want to quickly scaffold up a method that you know will have a complex return type down-the-road, but you're unsure what structure that object will take atm. Currently I am doing the below work around, which IMO is much much uglier than the OP's suggested ending-comma approach.
|
Beta Was this translation helpful? Give feedback.
-
Not everyone is familiar with source generators. Maybe a quick example is in order here.
From: Cyrus ***@***.***>
Sent: Tuesday, October 17, 2023 9:41 PM
To: ***@***.***>
Cc: John ***@***.***>; ***@***.***>
Subject: Re: [dotnet/csharplang] Support value tuples with single value using an extra comma in the list (example `var tuple = (int x,)`) (Discussion #4668)
you could always create a source-generator that does exactly that for you :)
i don't think there's any appetite for doing that by default. Especially as the constructor may throw, and implicit operators should not do that.
—
Reply to this email directly, view it on GitHub<#4668 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AFLXN2MPNRW2RD52LPR6KKDX74XVVAVCNFSM43EMP352U5DIOJSWCZC7NNSXTOKENFZWG5LTONUW63SDN5WW2ZLOOQ5TOMZQHE3DINI>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Sigh, I wish C# had this. I don't know why it wasn't put in in the first place. I have methods returning 5, 4, 3, or 2 values and I can name them nicely. But if I decide I want to remove one, bam I'm down to one return value and I have to totally switch to having an unnamed return value. Please implement this feature instead of more pointless stuff like "top level statements" that adds nothing of value. |
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.
-
Currently
C#
supportsValueTuple<T>
but not the syntaxbecause obviously the above is valid syntax for expressions.
I propose to allow single-valued tuples in a similar syntax to
Julia
with a comma after the one element.The intent is to be able to have a named variable in the
ValueTuple<T>
that isn'tItem1
and for consistency allow a comma at the end of the tuple value in general
Of course, this might cause visual confusion with discarded values when
(1,2,3,_)
means there are 4 elements in the tuple and(1,2,3,)
would mean there is 3 elements in the tuple.Beta Was this translation helpful? Give feedback.
All reactions