- Enter
Coercible
- What can be coerced?
- Updating the display function
- Type applications
- Coercibility is transitive
- Coercion via type parameters
- Coercing functions
- Coming up
In the previous lesson, we wrote this function:
We could envision having to write a lot of these, which would quickly get tiresome.
This is the kind of annoyance that hasn’t come up in this course because the examples have been small enough (we’ve only defined three newtypes), but can easily become a problem in a large codebase.
It gets worse if we have newtypes for other newtypes. Suppose our system has two kinds of login – normal users and administrative users – and we want to distinguish the password of an ordinary user from the password of an administrator, perhaps because we’re going to impose more stringent validation rules on administrator accounts.
We might want one set of “unwrappers” to convert each of these to Password
…
And then we may additionally want functions that unwrap both layers to get all the way down to the String
.
And we’d probably also like a composition of the constructors to coerce in the other direction as well.
This will get tiresome very quickly. Fortunately, GHC has a function called coerce
that can take the place of all of these conversions, and that’s what this lesson is about.