C# 7.0 introduced value tuples and also some language level support for them. They added the support of single and zero element tuples as well; however, I could not find out any scenario when they could be useful.
By ValueTuple.Create
overloads I can create any kind of tuples but the C# 7.0 syntax allows only at least two elements:
Microsoft (R) Roslyn C# Compiler version 2.8.3.62923
Loading context from 'CSharpInteractive.rsp'.
Type "#help" for more information.
> ValueTuple.Create()
[()]
> ValueTuple.Create(1)
[(1)]
> ValueTuple.Create(1, "x")
[(1, x)]
By tuple syntax:
> var t2 = (1, 2);
> var t1 = (1); // t1 is now int (of course)
> ValueTuple<int> t1 = (1);
(1,23): error CS0029: Cannot implicitly convert type 'int' to 'ValueTuple<int>'
> ValueTuple<int> t1 = new ValueTuple<int>(1);
> t1
[(1)]
I think I found the thread where this feature was requested but none of the code samples are valid in C# now and could not find any references in the planned features of C# 8.0 either, not even at the recursive tuple patterns.
In the request thread functional programming languages are mentioned. Is there maybe any functional language, which uses them now? I'm not an F# expert but its tuple reference does not mention any use of single and zero element tuples.
So the TL;DR questions:
- Are single and zero element tuples used in any (maybe functional) .NET language? I mean not by
Tuple.Create
or by the constructors but by native language support. - Are they planned to used in a future version of C#?
- Or are they there for "just in case", for future compatibility?
(1)
in C# is a breaking change due to the current parse rule. (That is why it evaluates to1
.) – Patrick Hofman