Advanced Elm Extensible Custom Types
Transcript from the "Extensible Custom Types" Lesson
>> Richard Feldman: This is a sort of a more concrete example of this obviously. Using it as a custom type we can say type article extraInfo. Usually, I'll call this extraInfo because it's some extra info that goes with the article. And it says article which has some record about the metadata that we know is always there.
[00:00:18] So, the title, the tags, and then in the real version, there are several other fields in there and then we have extra info which is the second piece info that goes with this variant and we don't know what its type is. So, in the case of a preview, it's gonna be one thing and in the case of a body, it's gonna be something else.
[00:00:33] So here are the types that I've defined for Preview and Full. So Preview says type Preview = Preview and essentially means there is no other info, there is no extra info, the extra info that is here is just empty. And then we have type Full, which has one variant which holds onto the body of the article.
[00:00:51] So the way this works out in practice is we have for example, a decoder of article preview, which will decode into one of these that has the metadata, and then just this empty preview thing for extra info. Which is to say no other interesting information, but because I've named it preview, this is more descriptive now.
[00:01:07] It says that I'm decoding a article that's a preview version that doesn't have the full amount of info. And the other one I have Decoder (Article Full), which both tells me that I have a full article, including the body, and also let's me extract that once I have one of these.
[00:01:22] So if I were to pattern match on this, and I have a preview, there would be no interesting stuff in here. The pattern would match on Preview, and I would say, yeah, well there's no body inside a preview, so can't do anything else. But if I'm pattern matching on one of these that is full, then I can say, I see I have a body inside there, I can get that, I can work with it, I can render it.
[00:01:40] So on the page that renders the entire article, we have a full which means we have the body and on the feed we only have the preview.
>> Richard Feldman: We can still access, we can write functions that let people access these things. So I can say title which takes an article a and returns a string, which is to say any article whether it's a preview article, or a full article, I always have access to the title.
[00:02:01] So this is a function that can be used in either context, it can be used on the feed, or it can be used on the view article page itself. And then I can also do the same thing with body. But in the case of body, I can only write this with this type.
[00:02:13] I can only say I will give you a body if you provide me with an article but it has to be a full article. I cannot give you a body if you give me an article preview. And also, I can't give you a body if give me an article A with an unbound type because I don't necessarily know if I have a body to give you.
[00:02:28] Only if you give me a full article can I definitely give you a body.