Purpose of 3-tuples

What are tuples with 3 elements ever useful for? I have only ever used ones with 2 items.

update usually evaluates to (Model, Cmd Msg) but if you want to send information upward for some reason, you can change that (Model, Cmd Msg, OutMsg).

Another, very similar case is when you are editing a context. There is the context, some state and possible effects. You will get into a similar (Model, Cmd Msg, Context) where instead of being a message, the third value is the potentially updated context.

There are also some exceptional contexts where you need to pattern match agains 3 values when implementing some logic.

I think one of the most common use cases is coordinates

type alias X = Int
type alias Y = Int
type alias Z = Int
type alias Coords = (X, Y, Z)

however, this opens the door to why tuples were limited to 3-tuples when there are use cases for more than that. I believe there was a blog post somewhere detailing the reasoning, but I still remember Elm 0.18 allowed for larger tuples

This is hinted towards when you attempt to create larger tuples.

For my taste, I think limiting the number of parameters in a function to 3 would also improve a lot of code. I try to aim for a maximum of 2 arguments with 3 being exceptional cases.

For what it’s worth, Deno is recommending a similar approach of 2 arguments for most functions https://github.com/denoland/deno/blob/master/docs/contributing/style_guide.md#exported-functions-max-2-args-put-the-rest-into-an-options-object

1 Like

I think a good rule for function arguments and tuples is: avoid ambiguity.

If you have a function that is Bool -> Bool -> Html msg you’ll have to go to the function definition to see what those variables actually are.

If it were instead { open: Bool, showBanner: Bool } -> Html msg it is a lot clearer what it is happening when reading it.

I generally agree on fewer arguments for functions, with one exception, for Html.lazy you often need to pass in multiple arguments to a function to maintain laziness. Creating a record on the fly would create a new reference and break laziness. Start with the easier to read code first and then optimize it if needed though.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.