5

Is it possible to write extension methods for F# tuples? For example, to add instance methods .Item1 and .Item2 (like System.Tuple) which are equivalent to calling fst and snd for 2-tuples?

5 Answers 5

5

The System.Tuple<'T1, 'T2> type that internally represents (2-element) tuples in F# actually already has properties Item1 and Item2, but these are hidden by the F# compiler. An obvious method to add extension members to a tuple does not do the trick, so I would not expect this to work (but there may be some workaround I'm not aware of).

Generally, I think pattern matching is preferable to members such as Item1, Item2 etc. (and C# 3.0 programmers often ask for pattern matching support when working with tuples :-)).

The reason is that pattern matching forces you to name things. Compare these two code snippets:

let (width, height) = tuple
width * height

and a version using properties:

tuple.Item1 * tuple.Item2

The second is a bit shorter, but definitely less readable.

1
  • Thanks Tomas. I agree, but am still interested if there is a workaround (without resorting to a new type as suggested below by bytebuster)?
    – mpeac
    Jan 2, 2013 at 4:00
3

Not perfect but I'm using this. (I borrowed original code from http://www.fssnip.net/6V and added small modification.)

[<AutoOpen>]
module TupleExtensions =
  type System.Tuple with
    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  

How to use it:

let t = (1, 2, 3)
let item1 = Tuple.Item1(t)

Tuple.Item1 defined here has advantage over fst: It is polymorphic for number of items. Once we write function which uses n tuple using these extension methods, we can extend it for n+1 tuple without modifying function body. Instead we have to modify argument type declaration. It is more effortless.

0

I think, what you're asking is not very functional way. You can make your own type with instance methods, but at the same time you are losing many aspects of functional programming, e.g. pattern matching.

Other than that, a DU seems to be the way to go:

type MyTuple<'T, 'U> =
    | MyTuple of 'T * 'U
    with
    member this.MyItem1 = match this with | MyTuple(x,y) -> x
    member this.MyItem2 = match this with | MyTuple(x,y) -> y

let x = MyTuple(42, "foo")
let y1 = x.MyItem1    // 42
let y2 = x.MyItem2    // "foo"

As @Tomas Petricek noted, you can't name the properties Item1 and Item2 since they already exist in System.Tuple<'T1, 'T2>. Attempting to do that will cause an error:

error FS2014: A problem occurred writing the binary [filename]: Error in pass2 for type [...], error: Error in pass2 for type MyTuple`2, error: duplicate entry 'Item1' in property table

0

You could also use the fst and snd functions to get the values you want (and obviously write your own for third, fourth, etc. if you really wanted to).

0

The workaround is to use C# style extension definitions.

This will work just fine:

open System.Runtime.CompilerServices

[<Extension>]
type TupleExtensions () = 
    [<Extension>] static member First((a,b)) = a
    [<Extension>] static member First((a,b,c)) = a

let x = (1,2).First()
let y = (1,2,3).First()

But I agree in that it's not a good idea to access the elements of a tuple through methods, pattern matching is the best way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.