Allow creating a tuple of ref structs #2584
Replies: 9 comments 6 replies
-
The only way this could even be allowed is to duplicate the |
Beta Was this translation helpful? Give feedback.
-
I believe (but am not sure) that ref structs are purely a language concept, not a runtime one, so I don't think that's true. Of course the language would have to make sure that such a tuple is never boxed or cast to ITuple. I imagine that's doable though. Alternatively, creating a RefStructTuple should also be perfectly doable. |
Beta Was this translation helpful? Give feedback.
-
They're not. For example, if you try to create a regular
|
Beta Was this translation helpful? Give feedback.
-
No, they are properly runtime supported afaik (at least on core). This would require allowing |
Beta Was this translation helpful? Give feedback.
-
where are we with this? |
Beta Was this translation helpful? Give feedback.
-
It will be usefull for Example - dotnet/runtime#30175 |
Beta Was this translation helpful? Give feedback.
-
Returning tuples from function (typically with 2-3 elements max) is a really great way to write cleaner and easier to read code> Ex: That's excellent. However, NOT Being able for a returned tuple element to be of type
I understand this would require some special treatment, but would ensure consistency in the langage and the source code we can write. Keeping the language consistent is imho important, else it's hard to understand/use, both for newcomers and experienced programmers. |
Beta Was this translation helpful? Give feedback.
-
I'm another one interested in this. |
Beta Was this translation helpful? Give feedback.
-
Something like this should be/is possible in C#13. public ref struct RefTuple<T1, T2>(T1 item1, T2 item2)
where T1 : allows ref struct
where T2 : allows ref struct
{
public T1 Item1 = item1;
public T2 Item2 = item2;
public void Deconstruct(out T1 item1, out T2 item2)
{
item1 = this.Item1;
item2 = this.Item2;
}
} |
Beta Was this translation helpful? Give feedback.
-
Tuples are often treated as collections of elements rather than a type in and of themselves by the language. For example see dotnet/roslyn#35695.
They are also very useful when it is necessary to return multiple values from a function.
Tuples are already special cased by the compiler.
For these reasons I think it would make sense to allow creating tuples containing ref structs. The tuples themselves would be considered to be ref structs.
Here are two use cases that I came across today:
And
Beta Was this translation helpful? Give feedback.
All reactions