Hi,
I have an opinion, which I dont think it all that uncommon, but maybe held to an extreme extent, which is that type aliases are unhelpful and even hurtful to codebases. Here are my primary examples:
type alias UserId =
String
type alias Users =
List User
type alias Validator a b =
a -> Result Error b
For me, when I see…
getUser : String -> Cmd Msg
getUser userId =
I can see that theres a thing called “userId”, and its of type String
. But when I see…
getUser : UserId -> Cmd Msg
getUser userId =
Now its a mystery as to what type userId
really is, and to find out, I need to take the manual step of seeing the definition of UserId
. Between the two examples, readers of my code have simply lost information by using the type alias UserId
.
And for what gain? As far as I can tell, none. Theres no type safety in UserId
, its just an alias. An email address is a valid UserId
. An entire work of Shakespeare is a valid UserId
.
Type safety is great. I think its great to control the creation and use of certain kinds of values. But I dont need type aliases for that. I can do these…
type UserId
= UserId String
getUser : UserId -> Cmd Msg
getUser (UserId userId) =
-- ..
getUser : { userId : String } -> Cmd Msg
getUser { userId } =
Whenever you want to give an alias to a type, you could just make a new type instead. Things that are so categorically different that they need distinct names, are just different categories to begin with, and therefore should live in your code as different types.
I would say that type aliases should never be used at all, if it were not for their usefulness in defining records.
type alias Model =
{ url : String
, seed : Random.Seed
-- ..
}
So, I propose the following changes to the Elm programming language…
0 Add a new syntax for naming records…
record alias Model =
{ url : String
, seed : Random.Seed
-- ..
}
…that is much like today’s type alias
syntax, except that it only works for records, and…
1 Remove Type aliases
What do you all think? Am I missing something?