Accessing a specific member in a F# tuple

Nikola Stjelja picture Nikola Stjelja · Mar 1, 2009 · Viewed 17.3k times · Source

In F# code I have a tuple:

let myWife=("Tijana",32)

I want to access each member of the tuple separately. For instance this what I want to achieve by I can't

Console.WriteLine("My wife is {0} and her age is {1}",myWife[0],myWife[1])

This code doesn't obviously work, by I think you can gather what I want to achieve.

Answer

Curt Hagenlocher picture Curt Hagenlocher · Mar 1, 2009

You want to prevent your wife from aging by making her age immutable? :)

For a tuple that contains only two members, you can fst and snd to extract the members of the pair.

let wifeName = fst myWife;
let wifeAge = snd myWife;

For longer tuples, you'll have to unpack the tuple into other variables. For instance,

let _, age = myWife;;
let name, age = myWife;;