Is there any way to use the type system to set a minimum length on a collection?
For example: a list with two or more objects. I could easily do the following:
type alias Data =
List Int
fromList : List Int -> Maybe Data
fromList list =
case list of
xa :: xb ->
Just <| list
_ ->
Nothing
And then not allow Data to be generated in any other way.
But having a List still implies the [] and [1] case, which could ultimately lead to confusion. So is there a smarter way to build a collection like this?
Yes, it is possible, and what @unsoundscapes suggests is indeed what is used in practice. As an example see List.NonEmpty.
The main caveat is that when introducing such a type, you’ll have to write all the convenience functions you know and love from ‘normal’ lists for this new data-structure. Because Elm has no dependently-typed features whatsoever, there is no way to automate this boilerplate for you .