3 people like it.

Overloads to get tuple items

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

Tuple overloads

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
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    

Example

1: 
2: 
3: 
4: 
5: 
6: 
7: 
//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
static member TupleUtils.Item1 : t:('a * 'b) -> 'a

Full name: Script.TupleUtils.Item1
val t : 'a * 'b
val x : 'a
static member TupleUtils.Item1 : t:('a * 'b * 'c) -> 'a

Full name: Script.TupleUtils.Item1
val t : 'a * 'b * 'c
static member TupleUtils.Item1 : t:('a * 'b * 'c * 'd) -> 'a

Full name: Script.TupleUtils.Item1
val t : 'a * 'b * 'c * 'd
static member TupleUtils.Item1 : t:('a * 'b * 'c * 'd * 'e) -> 'a

Full name: Script.TupleUtils.Item1
val t : 'a * 'b * 'c * 'd * 'e
static member TupleUtils.Item1 : t:('a * 'b * 'c * 'd * 'e * 'f) -> 'a

Full name: Script.TupleUtils.Item1
val t : 'a * 'b * 'c * 'd * 'e * 'f
static member TupleUtils.Item1 : t:('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 'a

Full name: Script.TupleUtils.Item1
val t : 'a * 'b * 'c * 'd * 'e * 'f * 'g
static member TupleUtils.Item2 : t:('a * 'b) -> 'b

Full name: Script.TupleUtils.Item2
val x : 'b
static member TupleUtils.Item2 : t:('a * 'b * 'c) -> 'b

Full name: Script.TupleUtils.Item2
static member TupleUtils.Item2 : t:('a * 'b * 'c * 'd) -> 'b

Full name: Script.TupleUtils.Item2
static member TupleUtils.Item2 : t:('a * 'b * 'c * 'd * 'e) -> 'b

Full name: Script.TupleUtils.Item2
static member TupleUtils.Item2 : t:('a * 'b * 'c * 'd * 'e * 'f) -> 'b

Full name: Script.TupleUtils.Item2
static member TupleUtils.Item2 : t:('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 'b

Full name: Script.TupleUtils.Item2
static member TupleUtils.Item3 : t:('a * 'b * 'c) -> 'c

Full name: Script.TupleUtils.Item3
val x : 'c
static member TupleUtils.Item3 : t:('a * 'b * 'c * 'd) -> 'c

Full name: Script.TupleUtils.Item3
static member TupleUtils.Item3 : t:('a * 'b * 'c * 'd * 'e) -> 'c

Full name: Script.TupleUtils.Item3
static member TupleUtils.Item3 : t:('a * 'b * 'c * 'd * 'e * 'f) -> 'c

Full name: Script.TupleUtils.Item3
static member TupleUtils.Item3 : t:('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 'c

Full name: Script.TupleUtils.Item3
static member TupleUtils.Item4 : t:('a * 'b * 'c * 'd) -> 'd

Full name: Script.TupleUtils.Item4
val x : 'd
static member TupleUtils.Item4 : t:('a * 'b * 'c * 'd * 'e) -> 'd

Full name: Script.TupleUtils.Item4
static member TupleUtils.Item4 : t:('a * 'b * 'c * 'd * 'e * 'f) -> 'd

Full name: Script.TupleUtils.Item4
static member TupleUtils.Item4 : t:('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 'd

Full name: Script.TupleUtils.Item4
static member TupleUtils.Item5 : t:('a * 'b * 'c * 'd * 'e) -> 'e

Full name: Script.TupleUtils.Item5
val x : 'e
static member TupleUtils.Item5 : t:('a * 'b * 'c * 'd * 'e * 'f) -> 'e

Full name: Script.TupleUtils.Item5
static member TupleUtils.Item5 : t:('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 'e

Full name: Script.TupleUtils.Item5
static member TupleUtils.Item6 : t:('a * 'b * 'c * 'd * 'e * 'f) -> 'f

Full name: Script.TupleUtils.Item6
val x : 'f
static member TupleUtils.Item6 : t:('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 'f

Full name: Script.TupleUtils.Item6
static member TupleUtils.Item7 : t:('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 'g

Full name: Script.TupleUtils.Item7
val x : 'g
val f : 'a * 'b -> 'a * 'a

Full name: Script.f
val f' : 'a * 'b -> 'a * 'a

Full name: Script.f'
type TupleUtils =
  static member Item1 : t:('a * 'b) -> 'a
  static member Item1 : t:('a * 'b * 'c) -> 'a
  static member Item1 : t:('a * 'b * 'c * 'd) -> 'a
  static member Item1 : t:('a * 'b * 'c * 'd * 'e) -> 'a
  static member Item1 : t:('a * 'b * 'c * 'd * 'e * 'f) -> 'a
  static member Item1 : t:('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 'a
  static member Item2 : t:('a * 'b) -> 'b
  static member Item2 : t:('a * 'b * 'c) -> 'b
  static member Item2 : t:('a * 'b * 'c * 'd) -> 'b
  static member Item2 : t:('a * 'b * 'c * 'd * 'e) -> 'b
  ...

Full name: Script.TupleUtils
static member TupleUtils.Item1 : t:('a * 'b) -> 'a
static member TupleUtils.Item1 : t:('a * 'b * 'c) -> 'a
static member TupleUtils.Item1 : t:('a * 'b * 'c * 'd) -> 'a
static member TupleUtils.Item1 : t:('a * 'b * 'c * 'd * 'e) -> 'a
static member TupleUtils.Item1 : t:('a * 'b * 'c * 'd * 'e * 'f) -> 'a
static member TupleUtils.Item1 : t:('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 'a
Raw view Test code New version

More information

Link:http://fssnip.net/6V
Posted:13 years ago
Author:Stephen Swensen
Tags: tuples