r/fsharp • • 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

8 comments sorted by

9

u/statuek Mar 04 '19 edited Mar 04 '19

I think you're missing the most common way of doing this: tuple deconstruction.

Let's say you have a tuple: let x = (1, 2)

You can deconstruct this into its parts like this: let a, b = x

Then, you can get the sum by: let y = a + b

I don't think people use fst and snd very often, especially since they don't work (iirc) when using n-tuples where n > 2


Moreover, be sure to NOT use tuples in excess. Short code is cool, but legible code is much more important. If it's not obvious what the components of a tuple should be without looking at the definition, maybe you shouldn't be using a tuple.

0

u/pure_x01 Mar 04 '19 edited Mar 04 '19

I know about deconstruction but that requires a whole new row for doing that. The ._X syntax is more succinct and readable. I want to be able to extract the values inline where it is being used.


Agree. I use it for succinct testing code where it is obvious what the tuples represent. I only use pairs as well. Readable code is king.

4

u/CanvasSolaris Mar 04 '19

It's not readable because it's not clear what _1 and _2 are. Also the presence of underscores indicates that this is doscouraged and not idiomatic

-3

u/pure_x01 Mar 04 '19

It's not idiomatic F# since it's not in the language. Its idiomatic scala and for a scala developer it's very clear what it does. If F# incorporates the syntax everyone would see it as idiomatic and understand exactly what it does. It's the same for non F# developers seeing ex: ":>" for the first time .

9

u/statuek Mar 05 '19

I dislike the ._N syntax deeply.
Don't make it easier for developers to do stupid things - they're good enough at it without bad language features.

1

u/BezierPatch Mar 09 '19

> but that requires a whole new row for doing that

Does it? How did it get into "x" in the first place? Do the deconstruction there.

1

u/jdh30 Mar 25 '19 edited Mar 25 '19
let y = (fst x) + (snd x)

Don't do that. IMO fst and snd shouldn'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, match expressions have a pattern in every match case, fun lambdas have a pattern as the argument and the short-hand function starts with a pattern. You have ample opportunity to use patterns to destructure everything!

FWIW, the problem with fst and snd is that they make 2-tuples (aka pairs!) special when they aren't special. So noobs get tripped up because they cannot do fst (1,2,3).

Standard ML had a #n tuple syntax:

fun max(pair: real*real):real =
    if (#1 pair) < (#2 pair) then
  (#2 pair) else (#1 pair);

But I recall that tripping me up back when I was a noob.

1

u/pure_x01 Mar 25 '19

Thanks. That #INDEX syntax would be nice to have