Language support for KeyValuePair<TKey, TValue> struct and tuple mappings #6625
Replies: 11 comments
-
Similar functionality is explicitly mentioned in the meeting notes here: dotnet/roslyn#11205 However they are looking at much more general purpose syntax which works with existing types rather than defining new forms of operators and the like. Your proposal adds a lot of new language concepts for a rather narrow feature and I don't think it's worth it. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour this narrow feature is a starting point. I suggested general approach completely independent from Tuple and KeyValuePair types. "Tuple mapping" is a similar concept to tuple-like constructors, but it generalize approach and merge it with tuples as it allow not only construction but assignment of existing tuples to variable of any type which support tuple mappings. As for keyed types it's not so "narrow" feature. Key/Value pattern is used massively and currently is tied to KeyValuePair struct. One obvious field of use for keyed types is a SQL-mapped ORM objects as any ORM entity have primary key and used with key in almost any data processing algorithms. Operator overloading is a natural way to handle such features. |
Beta Was this translation helpful? Give feedback.
-
Why would you need new general purpose tuple operators when you could just use the existing implicit conversion operators? public static implicit operator Class2<T1, T2>((T1 field1, T2 field2) tuple)
{
return new Class2<T1, T2>() { Field1 = tuple.field1, Field2 = tuple.field2 };
} The KVP-specific elements of this proposal involve adding some very non-C# syntax to C#. I'm not sure how you propose that those generic members would work given the nature of generics in the CLR. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour because (T1, T2) tuple in this case is a concrete type mapped to |
Beta Was this translation helpful? Give feedback.
-
I suggest approach similar to |
Beta Was this translation helpful? Give feedback.
-
The mapping to an interface constraint isn't enough, you can't dot into a generic type parameter member to ascertain another generic type parameter. public IKeyedDictionary<T, TK, TV> where T : IKeyValuePair<TK, TV> { } It wouldn't be a good idea to automatically expand the arity as |
Beta Was this translation helpful? Give feedback.
-
@HaloFour to avoid implicit interface arity expansion it will be better to define Updated issue thread starting post. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour So, to sum up, on CLR level
If you do not define T.! or T.* type arguments it should be translated to
So you can assign So,
and if you implement other interface
would translate to
If you don't define T.! or T.* type arguments it mean you have no interest in its types and it would default to And another example:
will be translated to
|
Beta Was this translation helpful? Give feedback.
-
@Opiumtm So that would render those syntax changes unnecessary. You're not talking about a situation that occurs frequently enough to justify having to invent completely alien syntax just to save maybe a dozen keystrokes. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I'm talking about very common and especially painful to use in real code pattern. Key/Value pairs are used in many places including modern Windows Runtime APIs. Keyed objects is used everywhere if object have unique ID. It would add generalized syntax to such keyed objects and key/value pairs. |
Beta Was this translation helpful? Give feedback.
-
Strongly dislike |
Beta Was this translation helpful? Give feedback.
-
KeyValuePair is very often used in C# code in many scenarios.
As there is
(k, v)
tuple syntax support, I suggest same support for KeyValuePair widely used struct.dotnet/roslyn#11530
As KeyValuePair and Tuple is a common pattern and sometimes there is custom Tuples or Keyed structs implemented, I suggest generalized built-in support for such tuple patterns and generalized support for keyed classes/structs at language level.
For such purposes I suggest new overloadable operators.
For tuple mappings (enable tuple-syntax initializing and tuple assignment)
and if class is generic:
This will allow such things:
So,
KeyValuePair<TKey, TValue>
should also support tuple operator and instead ofnew KeyValuePair<string, string>("key", "value")
you can just write("key", "value")
if you're returning KeyValuePair from function, assign it to variable or use it as parameter for method.And for object keys support.
Support for "value" operator
Extract types of keys and values at compile-time
Use types of keys and values in generics
T.!
meaning type of key andT.*
meaning type of valueAnd implicit (or explicit) operator conversion from keyed objects for existing APIs compatibility
Also added
<T>
forimplicit<T>
operator, so implicit conversions can accept generic source object argumentBeta Was this translation helpful? Give feedback.
All reactions