Advanced Elm

The Never Type

Richard Feldman

Richard Feldman

Vendr, Inc.
Advanced Elm

Check out a free preview of the full Advanced Elm course

The "The Never Type" Lesson is part of the full, Advanced Elm course featured in this preview video. Here's what you'd learn in this lesson:

Richard shows how the Never type can be used to describe Tasks that are guaranteed never to fail.

Preview
Close

Transcript from the "The Never Type" Lesson

[00:00:00]
>> Richard Feldman: For the purposes of this library, it doesn't actually matter how Never is defined. The whole point is that it's not something that we have access to, right? It's not our message and it's not unbound. It's something else and that's enough to get the characteristics we want. It will be enough for Never to be an opaque type.

[00:00:16]
To say that Never is something that she used to find in her package and does not expose any variants for so, it's opaque. We don't know how to one, we just know that it exists and we don't have a way a make one so that's enough. As it happens, Never goes even further than that.

[00:00:33]
Here's how Never is defined. type Never = OneMore Never. So even in the module where this thing is defined, you can't make one. And to prove it to you, let's try and make one. So we have one variant. So that's the only way we can try and make one.

[00:00:49]
And that one variant takes a Never. How do we make a Never? Well, we have to use this variance. So we'll call myNever : Never, and we're gonna call OneMore, and what do we pass that? We have to pass that a Never. How do we get a Never?

[00:01:01]
Okay, well, we call OneMore. How do we get a Never to pass that one? We call OneMore. It never ends, it's not possible to finish this expression with anything that's ever gonna compile because it refers to itself. You have to provide itself in order to get one. It's a cache 22 type if you will.

[00:01:20]
So this is essentially a type that is self documenting that by the way the entire purpose of this type is to prevent anyone from ever making one. And essentially, this is the use case for Never, is for use cases like Tess's where what you want to say is not so much, please provide me one of these, but rather, please provide me with something that still has an unbound type variable.

[00:01:44]
Now, you can't do this by asking for an unbound type variable, cuz if you ask for an unbound type variable, what that says is, give me anything. I will accept absolutely anything you want. What this says is, give me something that is still unbound, that's the only thing I'll accept.

[00:01:59]
>> Richard Feldman: So essentially by doing this she has ruled out any possible thing that you could give this argument to paragraph other than these two things. Either a list of attributes that are still unbound or being one step more general, it lists where the entire list is unbound. So empty lists or a list of attributes, all of which do not produce messages.

[00:02:22]
Those are the only two possible types that this will accept.

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