Transcript from the "Custom Types Review & Q&A" Lesson
>> Richard Feldman: So to recap, we started by looking at case-expressions. So we've got case msg of, and then we've got the different branches, each of which is matching against that msg. We've got one use of custom type is numerations, such as Bool. So we just have two variants there, true and false.
[00:00:15] Another use case for custom types is containers where we have not just an enumeration but rather we've got variance that in some cases hold on to additional data in addition to just specifying what the variant itself is. So start to clickedPage by message. Click to tab and any combination of those that we wanna do.
[00:00:35] And finally we saw variant functions, which is to say how we actually instantiate one of these things. So case expressions are how we unwrap them, functions are how we create them. So you say, clicked page, you call that even though its got a capital letter as a function passing the argument that it's going to end up being held inside the custom type.
>> Speaker 2: Returning the list in the context of custom types, can you mix list item types if you annotate them with a custom type that has int and string variants? So like-
>> Richard Feldman: Yes.
>> Speaker 2: Okay.
>> Richard Feldman: This is a fantastic insight. Yes, absolutely, you can. So the question was, can you get a list that actually holds onto mixed types if you wrap them in a custom type.
[00:01:18] Absolutely, that is how you do it, in fact. So let's say I wanted to have a list, which contains possibly a mix of strings and ints. I can make a custom type called string or int, which has two variants. One called it's a string, which holds onto a string, and other called, it's an int, which holds onto an int.
[00:01:36] If I have a list of those, none of the problems that we saw earlier with iteration happened in that case. Because if I'm writing a function, let's say I call list.map on that. I'm saying, yeah, I need to handle each of these elements. Well, it's no problem if they're wrapped in a custom type, because the custom type is going to force me to handle each possibility.
[00:01:56] It's going to say, yeah, you've enumerated exactly which possibilities happen here. Either it's a string or it's int, and the case expression is gonna say, okay, well now handle both of those possibilities. You have to handle the caseload as a string and also we have handle the case where was an int.
[00:02:22] But in Elm it can't happen if you wrap it in a custom type because you have to write the case expression that has to handle all of the different possible variants, which means you will definitely have logic that says, if it's a string, do this. If it's an int, do this.
[00:02:36] So none of those downsides exist if you do it that way, and that's exactly what you'd do if you wanted that. Great question, yeah.
>> Speaker 2: Going back to your exercise, why doesn't the type alias article collide with the import article?
>> Richard Feldman: Why doesn't the type alias article, so this is the part four exercise?
>> Speaker 2: Yeah.
>> Richard Feldman: Why doesn't the type alias article conflict with the import article? Short answer there is that article is the name of a module. Inside that module, there are various values and potentially, various types. I guess I could just jump back to it real quick and trust that our excellent video editors will figure out how to make this look coherent [LAUGH].
[00:03:22] So, we imported article. When I see this, that means I now have this article module in scope which means I can now say article dot whatever and that's going to refer to something that exists inside the article module. If I open up article.elem, we can see that they're actually is no type in here named article.
[00:03:41] Maybe if I wanted to I could say type alias article equals. And like to find that in here and I could expose that as something that is exposes. And then I could say import article, exposing article and then I could potentially get a conflict. But, just from importing the module, that's no problem.
[00:04:01] Importing the module just basically gives me a new name space to work with. It doesn't actually change what's in scope yet. Exposing does that. Exposing brings more stuff into scope. But just importing the article itself doesn't do that.