Introduction to Elm, v2 Aliases
Transcript from the "Aliases" Lesson
>> Richard Feldman: Here's how we could annotate the model that we used in the previous exercise. We could write out this entire elaborate nested record, and this is perfectly accurate. If we wanted to put this in our program, Elm would accept it, it would say, yes, I agree, that is what your model looks like.
[00:00:13] It's a record of a selectedTag field, which is a String, a List of articles, which is a List of also records. Each of those records has a title String, tags, which is a List of Strings, and then body, which is also a String. But as you can imagine if your program gets kind of big, you don't want to write all this out all the time.
[00:00:29] That's just a lot of stuff to be repeating, especially if you have multiple different places where a model is being used. You don't have to copy and paste all that around, fortunately, we don't have to. We can do what's called a type alias, which is a way to give a name to a type.
[00:00:45] So just like we can have declarations, where we say user name equals, quote, RTFeldman, that's giving a name to a value. Type alias is a way to give a name to a particular type. And it's most often used with more elaborate types, sort of of compound types like this that have other types inside of them.
[00:01:01] Like records or parameter types. Once we define our type alias for Article, then the type alias for Model gets much more concise. Now we can say selectedTag is a String, and articles is just List Article. It's a list of articles and then we've defined elsewhere what it means to be an article.