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

The "Parameters" 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 exaplains why Lists in Elm have to have a consistent type, and how to apply type annotation using type parameters.

Preview
Close

Transcript from the "Parameters" Lesson

[00:00:00]
>> Richard Feldman: We can also have a list. Notice, by the way, that this does not say names is a list, it's more specific than that. It says that names is a list of strings, that's how you can read this type annotation. There is the list, then there is a space, or some white space, followed by the thing that is the type of its element, the elements in the list.

[00:00:20]
So as we talked about earlier, lists in Elm have to have a consistent type, every element has to be the same type. And that's captured in the type annotation. We say, this is not just a list, this is a list of strings. That's not optional, you have to say here is what kind of list it is.

[00:00:35]
That makes List what's known as a parameterized type. So it's a parameterized type because saying List by itself is not enough. You have to give it some extra piece of information, which is known as the type parameter. So in this case, String is the type parameter that corresponds to the parameterized type of List.

[00:00:55]
>> Richard Feldman: And the way that we would read this out loud is this is a list of strings. You can also have a list of bools, that's totally valid. Or a list of floats, also totally valid. But just like when I have to add an accurate annotation to any of my primitives that are not parameterized types, I also have to be accurate with not just the parameterized type in general, but also its type parameter has to be accurate.

[00:01:20]
So if I change this from List Float to List Int. But it's still actually holding onto floats, that is to say numbers that have a decimal point in them and are not integers. I will get a compile error, this will not compile. It'll say, nope, that's an inaccurate annotation.

[00:01:33]
I know that these is a list of floats, you cannot call that a list of ints. It won't accept that.

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