elm

Language Idea: Limit custom types to three arguments

robinheghan

Robin Heggelund Hansen

Posted on September 23, 2018

Language Idea: Limit custom types to three arguments

Language Idea is a new series where I note down some ideas I have on how Elm could improve as a language. Each post isn't necessarily a fully thought out idea, as part of my thinking process is writing stuff down. This is more a discussion starter than anything else.

In Elm 0.19 you can no longer create tuples which contains more than three elements. The reasoning behind this is that it's easy to lose track of what the elements represents after that. As an example:

(0, 64, 32, 32)
Enter fullscreen mode Exit fullscreen mode

Is a bit more cryptic than:

{ xPosition = 0
, yPosition = 64
, width = 32
, height = 32
}
Enter fullscreen mode Exit fullscreen mode

The rule here is quite simple, if you feel yourself in need of a tuple containing more than three elements you're better of using a record with named fields.

There is, however, another way in which you can work around this restriction in Elm 0.19.

Custom Types

Yes. Custom types can still contain more than three parameters. Which is strange, as there is little to separate tuples from custom types with the exception of syntax and comparability.

Let me be clear though, you should prefer records with named fields. Names provide basic documentation, and records let you forget about what order parameters are in. I'd say that even when you're using custom types, you should always limit them to three (maybe even two) parameters and use records if you need more than that.

As to why Elm allows more than three parameters in custom types; I don't know. It might just be an oversight.

So here is a question: should Elm limit the number of parameters in custom types to three, as it does with tuples?

I think it should.

Edit: The idea here is that one can do type Person = Person { name : String, age : Int } instead of type Person = Person String Int as it would make it more obious what each parameter is. It should then be encouraged to use records within custom types if there are many fields to store within the custom type.

💖 💪 🙅 🚩
robinheghan
Robin Heggelund Hansen

Posted on September 23, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related