Home
Insert
Update snippet 'Overloads to get tuple items'
Title
Description
The static representations, unlike the encoded System.Tuple<_,...,_> forms, of tuples do not have ItemX properties and therefore static structural constraints cannot be used to obtain the nth item of a tuple. Here we give a utility with overloads for obtaining the nth item of a tuple. However, type inference is undermined in the presence of overloads
Source code
// [snippet:Tuple overloads] type TupleUtils = static member Item1(t) = let (x,_) = t in x static member Item1(t) = let (x,_,_) = t in x static member Item1(t) = let (x,_,_,_) = t in x static member Item1(t) = let (x,_,_,_,_) = t in x static member Item1(t) = let (x,_,_,_,_,_) = t in x static member Item1(t) = let (x,_,_,_,_,_,_) = t in x static member Item2(t) = let (_,x) = t in x static member Item2(t) = let (_,x,_) = t in x static member Item2(t) = let (_,x,_,_) = t in x static member Item2(t) = let (_,x,_,_,_) = t in x static member Item2(t) = let (_,x,_,_,_,_) = t in x static member Item2(t) = let (_,x,_,_,_,_,_) = t in x static member Item3(t) = let (_,_,x) = t in x static member Item3(t) = let (_,_,x,_) = t in x static member Item3(t) = let (_,_,x,_,_) = t in x static member Item3(t) = let (_,_,x,_,_,_) = t in x static member Item3(t) = let (_,_,x,_,_,_,_) = t in x static member Item4(t) = let (_,_,_,x) = t in x static member Item4(t) = let (_,_,_,x,_) = t in x static member Item4(t) = let (_,_,_,x,_,_) = t in x static member Item4(t) = let (_,_,_,x,_,_,_) = t in x static member Item5(t) = let (_,_,_,_,x) = t in x static member Item5(t) = let (_,_,_,_,x,_) = t in x static member Item5(t) = let (_,_,_,_,x,_,_) = t in x static member Item6(t) = let (_,_,_,_,_,x) = t in x static member Item6(t) = let (_,_,_,_,_,x,_) = t in x static member Item7(t) = let (_,_,_,_,_,_,x) = t in x // [/snippet] // [snippet:Example] //f is infered as 'a * 'b -> 'a * 'a let f t = let x,_ = t in x,x //without the type annotation, you will get the error: //"A unique overload for method 'Item1' could not be determined //based on type information prior to this program point" let f' (t:_*_) = let x = TupleUtils.Item1(t) in x, x // [/snippet]
Tags
tuples
tuples
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update