Check out a free preview of the full Introduction to Elm, v2 course

The "Variants & Booleans" Lesson is part of the full, Introduction to Elm, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Richard goes into detail about how to create custom type variants. Booleans are also revealed to be a custom type in Elm.

Preview
Close

Transcript from the "Variants & Booleans" Lesson

[00:00:00]
>> Richard Feldman: This is another way of representing our three possible tags. This is a custom type. So, we use this type keyword, and then we give a name to that type, and then we specify the different possibilities. So here we're saying, tab has three possibilities. These are known as variance.

[00:00:15]
One variance is your feed, another is global feed, another is tag feed. A way that I can instantiate these is I can say, okay, I'm gonna make something called yours, and I'm gonna say, that is equal to YourFeed. So YourFeed is actually a value. It's a value whose type is Tab.

[00:00:31]
That's why it's a custom type. We've created a new type out of thin air. So type alias, in contrast, is just giving a name to an existing type. Custom types really are making a brand new type that did not exist before. So, here we're saying yours is equal to YourFeed.

[00:00:48]
That means, that this now has the value YourFeed and its type is Tab. We can do the same thing with global, and these are now different values. So in some languages when you do something like this, it will say, secretly under the hood, this is just a number.

[00:01:05]
This is the number zero, this the number one, this is the number two, and we're just giving names to them. That's not how it is in Elm. In Elm, it really is the case, these are just completely distinct values. Like if I said, yours equals equals global, that would return false.

[00:01:18]
It would say these are both tab values, but they're different tab values. They're not equal to one another. Same thing with Tab. If I were to make a new one of these, I would say, yeah, a TagFeed is not the same thing as a GlobalFeed, which is not the same thing as YourFeed.

[00:01:34]
YourFeed is equal to YourFeed, so if I said yours == yours, that would return true. If I said global == global, that would return true. And I don't need to use these names,I just wrote them out,so that I could annotate them with type to demonstrate how this relate.

[00:01:47]
But if I just said capital Y YourFeed == capital G GlobalFeed, that would return false. But If I said capital Y YourFeed == capital Y YourFeed, that would return true. The reason for the capital letters here, is that these are sort of reserved for Types and type annotations and custom type variants like this.

[00:02:11]
So in Elm, if you're a defining a new value, like you're giving it a name, it has to start with a lowercase letter. If you're defining a new variant,that starts an uppercase letter. We'll get to why that is momentarily. But, this is sort of the custom type itself tab is the Type, And these are the variants, these three are variants of the one ones that we defined.

[00:02:31]
We can define as many or as few of those that we want with just additional pipes and additional names. These language feature has various names and different languages, sum types, union types, which is what they used to be called in previous editions of ADT, F# uses algebraic data types.

[00:02:48]
F# calls them discriminated unions, lots of different names for these things. So, this is one example of a custom type. Another example of a custom type that we've seen before is Bool. This is how Bools defined,it has two variants, one is called true, the others called false.

[00:03:05]
And that's why Bool is True and False are capitalized in Elm, is because this is a custom type. That's all it is. The essence of a custom type, is to define these sort of different scenarios and just say, here's what they are. These are different. I'm not necessarily ascribing any particular underlying representation to them.

[00:03:23]
I'm just saying these values are different.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now