Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are single and zero element tuples good for?

Tags:

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?
like image 428
György Kőszeg Avatar asked Aug 07 '18 09:08

György Kőszeg


People also ask

What is a single element tuple?

Tuple with one element. If the tuple contains only one element, then it's not considered as a tuple. It should have a trailing comma to specify the interpreter that it's a tuple. Tuple examples. tupleWithOneElement = ( "hello" , ) # Notice trailing comma.

What is a single element tuple Python?

A tuple in Python is a collection of ordered and unchangeable items of the same data types stored in a single variable. We can write tuples with round brackets ()

Can tuple has only one element?

To create a tuple with only one item, you have add a comma after the item, otherwise Python will not recognize the variable as a tuple.

What option defines a zero element tuple?

Zero Element Tuples In Python, zero-element tuples look like: () In this form, unlike the other tuple forms, parentheses are the essential elements, not commas.


2 Answers

What good is a 0-tuple?

A 2-tuple or a 3-tuple represent a group of related items. (Points in 2D space, RGB values of a color, etc.) A 1-tuple is not very useful since it could easily be replaced with a single int.

A 0-tuple seems even more useless since it contains absolutely nothing. Yet it has properties that make it very useful in functional languages like F#. For example, the 0-tuple type has exactly one value, usually represented as (). All 0-tuples have this value so it's essentially a singleton type. In most functional programming languages, including F#, this is called the unit type.

Functions that return void in C# will return the unit type in F#:

let printResult = printfn "Hello"

Run that in the F# interactive interpreter, and you'll see:

val printResult : unit = ()

This means that the value printResult is of type unit, and has the value () (the empty tuple, the one and only value of the unit type).

Functions can take the unit type as a parameter, too. In F#, functions may look like they're taking no parameters. But in fact, they're taking a single parameter of type unit. This function:

let doMath() = 2 + 4

is actually equivalent to:

let doMath () = 2 + 4

That is, a function that takes one parameter of type unit and returns the int value 6. If you look at the type signature that the F# interactive interpreter prints when you define this function, you'll see:

val doMath : unit -> int

The fact that all functions will take at least one parameter and return a value, even if that value is sometimes a "useless" value like (), means that function composition is a lot easier in F# than in languages that don't have the unit type. But that's a more advanced subject which we'll get to later on. For now, just remember that when you see unit in a function signature, or () in a function's parameters, that's the 0-tuple type that serves as the way to say "This function takes, or returns, no meaningful values."

like image 196
rmunn Avatar answered Nov 04 '22 13:11

rmunn


I can't think of a use case for one element tuple. EDIT - As pointed out the referenced page mentions one element tuples as a way to name return parameters, which sounds actually useful, although this is probably C# specific.

Zero element tuple is in functional languages also known as unit. It's sort of equivalent of void in C#, which some (most?) functional languages don't have.

The difference is that () is an actual value, which you can do stuff with like hold a list of, pattern match etc. Functional languages need this because a function must return something and if you want to return "nothing" you have to do it explicitly by returning (). You can also write a function that accepts unit as a parameter, which basically means it's a value with delayed execution, most likely with side effects:

let x = getFromDb() // will read DB
stream.Flush() // will change state
like image 35
DevNewb Avatar answered Nov 04 '22 13:11

DevNewb