Associated/Replacement Identifiers for Generic Arguments #7612
Unanswered
daiplusplus
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
See: #1883 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the (perennial) discussions over "why can't we use 1-tuples for named-values?" I see a common use-case is to attach meaningful names/identifiers to scalars without wanting the overhead of having to define a new type (especially one that holds only a single value - though the recently-introduced
record struct
types do help here) - or having to compromise the design by introducing a 2-tuple just so they can use it like a 1-tuple (e.g.List<( Int32 userId, Byte _ )> listOfUserIds = ...
).In my own experience, instead of the above, I often find myself just using
/**/
-comments to add labels to generic type-arguments, for example, for a list ofInt32 userId
values, I'd haveList<Int32/*userId*/> userIds = ...
orDictionary<String/*zipCode*/,String/*countyName*/>
, for example....then I realised that the C# language could be extended to permit an identifier or label to be used following a generic type argument and expose that label as a replacement identifier for parameters and member-names (even if it's "faked" by storing the name in an attribute at compile-time; just like how C# already does this with
ValueTuple
).So if we could do this, then a hypothetical tweaked
List<T>
might look like this:And used like so:
This could also work for member names too, if we start-off with the (fantastic)
OneOf<>
library:...then the members of
dataSource
would have "virtual-names" (just likeValueTuple
) matching those identifiers (can we have auto-PascalCasing yet?) instead of having to use the generic-namedIsT0
IsT2
,IsT2
,TryPickT0
,TryPickT1
members:Beta Was this translation helpful? Give feedback.
All reactions