Advanced Elm Advanced Elm

Extensible Data Exercise

Check out a free preview of the full Advanced Elm course:
The "Extensible Data Exercise" Lesson is part of the full, Advanced Elm course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to return and convert data using extensible data principles.

Get Unlimited Access Now

Transcript from the "Extensible Data Exercise" Lesson

[00:00:00]
>> Richard Feldman: Let's move on to the exercise for part two. Down here that part two, open that up
>> Richard Feldman: put up a okay. Now open source article.elm and all there. Okay, so essentially, we have a couple of to-dos here. And they have to do with this extensible data idea.

[00:00:26] So we have Article Full and Article Preview. And in this example, we don't quite have the complete implementation of these. We have the types mapped out, where we have the Article, itself. Which is defined as type Article a, which we called in the other example extraInfo. We can call it that as well.

[00:00:45] Totally fine. Then we have this internals type and internals, is essentially a type alias for that particular chunk of data inside that record. The reason I like to do type alias called Internals, for this rather than inlining the record directly inside the custom type definition, the way that it is on the slide.

[00:01:08] Is that this way, I get to use this the automatic record instructor that comes from the type alias when I'm decoding this thing. So, we'll get into that in part 7, but suffice it to say that that's a common pattern that I use and that is the answer to a question that somebody asked in the last exercise, and about when to use that or when not, if you're using it with a decoder it's a very nice thing.

[00:01:29] So that's the structure of this article. Now as we previously discussed extra info, can either hold onto the value preview, or the value full, and if it's holding onto a full, then we have the body inside of that. Now that's what we've got but our implementation is not complete yet, because we don't have a couple of these functions filled in yet.

[00:01:52] So we have this function called body, which says I take an article full and return a body, that's a publicly exposed function. That's part of the public API. But it currently doesn't do anything, it just always returns an empty string. So that's no good, especially because sting is not actually a body.

[00:02:05] So it's not gonna compile. And in fact if we try to compile this, what we get is the pilot type errors. [LAUGH] So, we're gonna have to fix these one at a time. So that's the first to-do, is make it actually return the article's body, the second one, is this one converts from a preview to an article full.

[00:02:24] So this comes up in I forget which part of the code base, but basically we need to sort of upgrade it. And it says okay, well if you've got a preview, and you can provide a body for me, well, then putting those two together I can give you an Article Full.

[00:02:36] So this is sort of the function that does that conversion for the caller. And finally we have, [INAUDIBLE] TODO. Right, use Body.decoder which is type of Decoder Body, to decode the body into this Article Full. So this is something where we have two different decoders. We have a preview decoder, which decodes the preview article and then we have a full decoder, which decodes full article.

[00:02:59] So we're gonna implement the latter of those and you can see in the preview decoder. We, first we do the internals decoder and resolve all of that, and then we separately always hard code this to say, okay well we're just gonna stick a preview in there and then move on.

[00:03:13] But in this one, we're not gonna do that we're gonna actually decode the body and then put that into the resulting data structure.