Given the tuple:
let tuple = (true, 1)
How do i use this tuple in a conditional?
Something like this:
if tuple.first then //doesnt work
or
if x,_ = tuple then // doesnt work
I don’t want to do this:
let isTrue value =
let b,_ = value
b
if isTrue tuple then // boring
Is there a nice way to evaluate a tuple value inside a conditional without creating a seperate function ?
The
fstfunction can help you out here.An example:
There’s also an
sndfor the second element.Another alternative is to use pattern matching:
This can let you match on more complex scenarios, a very powerful construct in F#. Take a look at the Tuple Pattern in the MSDN Documentation for a few examples.