DEV Community

Discussion on: Help design a language: What about tuples without commas?

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

I like the way F# supports both \n and ; as separators for lists and arrays. (I do wish it were comma instead of semi-colon.)

Here's a list example. oneOf is given a list with 3 items. stopWith is given a list with 2 items.

let apiRoutes =
    oneOf [
        GET >=> path "/" >=> handler root
        POST >=> path "/upload" >=> requireAuth >=> handlerAsync upload
        stopWith [
            statusCode 404
            contentText \\_(ツ)_/¯"
        ]
    ]

But I typically only use semi-colons to separate inline items. Here is an array example. Notice the last item can have an optional separator on the end:

let storyPoints = [| 1; 2; 3; 5; 8; 13; 21; |]

Since either works as a separator, you can also use both.

let board =
    [ O ; X ; O
      X ; X ; O
      X ; O ; X ]

You can also turn it into the Elm-like syntax if you really want.

let elements =
    [ i [ class "warning icon" ] []
    ; div [] [ text "asdf" ]
    ; div [] [ text "fdsa" ]
    ]

I'm not a fan of this because cutting/pasting the first line gets weird (e.g. moving it to the bottom). Git can also get confused when you swap the first line for another and merge with someone else's changes.

Note: F# actually requires commas for tuples. Probably because tuples are defined with parens, which would be hard to disambiguate from an expression with newlines.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

That git requirement/problem is in my mind. I'd like to ensure that the source files are source control friendly.

I never thought about using ; to separate lists, though there's no real reason why I would distinguish between ; and ,. Just a classic syntax I guess, though semi-colons seem "heavier" than commas.

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

In the rare occasion that I have inline list elements, I instinctively use commas then have to go back and replace with semi-colons. The compiler thinks I have one list item that is a tuple, since commas indicate tuples in F# (even without parens).

I do like the range of choices that are given by using either a separator char or newline. However in practice, I nearly always use the first style (newlines only). It is the most copy-paste and git friendly, and secondarily I like the visual flow.

Thread Thread
 
mortoray profile image
edA‑qa mort‑ora‑y

I think the inline versus multiline choice depends on what type of code you are writing. Anything that involves a logical list of items I prefer newlines.

var items = [
  item_one,
  item_two(args),
  1 + 2,
]

But, sometimes I have little tuples, like points, that would be burdensome, and unclear to do multiline:

var a : point = [1,2]
var b = pt_a + [3,4]