Accessing a specific member in a F# tuple

前端 未结 4 652
南旧
南旧 2021-02-03 18:20

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 ac

4条回答
  •  没有蜡笔的小新
    2021-02-03 18:49

    Another quite useful thing is that pattern matching (just like when extracting elements using "let" binding) can be used in other situations, for example when writing a function:

    let writePerson1 person =
      let name, age = person
      printfn "name = %s, age = %d" name age
    
    // instead of deconstructing the tuple using 'let', 
    // we can do it in the declaration of parameters
    let writePerson2 (name, age) = 
      printfn "name = %s, age = %d" name age
    
    // in both cases, the call is the same
    writePerson1 ("Joe", 20)
    writePerson2 ("Joe", 20)
    

提交回复
热议问题