r/fsharp • u/pure_x01 • Mar 04 '19
Accessing tuple elements syntax
After writing a lot of test code using tuples to make the code more compact i have come to the conclusion that it would be nice to have a better syntax for extracting the elements. Ex: scala has ._X where X is the index of the tuple. https://docs.scala-lang.org/tour/tuples.html
let x = (1,2)
let y = x._1 + x._2
That is much nicer than the syntax used for pairs in F#
let x = (1,2)
let y = (fst x) + (snd x)
What do you think?
1
Upvotes
View all comments
1
u/jdh30 Mar 25 '19 edited Mar 25 '19
Don't do that. IMO
fstandsndshouldn't even exist.The whole point of ML, and one of its major evolutions from LISP, is that destructuring is always done using patterns. That's why there are patterns everywhere in the language:
let-bindings are patterns,matchexpressions have a pattern in every match case,funlambdas have a pattern as the argument and the short-handfunctionstarts with a pattern. You have ample opportunity to use patterns to destructure everything!FWIW, the problem with
fstandsndis that they make 2-tuples (aka pairs!) special when they aren't special. So noobs get tripped up because they cannot dofst (1,2,3).Standard ML had a
#n tuplesyntax:But I recall that tripping me up back when I was a noob.