Type Parameters in Reason

Nik Graf
InstructorNik Graf
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

In this lesson we will explore a bit more advances type system feature: type parameters. They are similar to generics in other languages. We create our own types using type parameters to demonstrate why they are useful.

Instructor: [00:00] Types can accept parameters, similar to generics in other languages. In fact, we encountered them a couple times already. For example, when creating a list, you need a type list, receiving strings. String, in this case, is a type parameter.

[00:17] Here, a list of integers. In this case, the compiler even invert the type parameter integer. Pretty cool. Let's take a step back and explore why parameterized types are important. In this case, we create two coordinate types, one containing integers, the other containing floats. Now we can use them.

[00:43] Using type parameters, we can create the type that has coordinates instead, that accepts one type parameter. Here, we declare that type parameter A of coordinate must be the same for the first and the second type in the tuple of two elements.

[00:57] It's as if type is a function that takes in parameters and returns a new type. From this point on, we can use the type coordinate. Here, we basically apply the coordinate type function, and return a type tuple with two integers. Trying the same construct using float works, as well.

[01:15] The hopefully obvious benefit is that we can kill repetition when creating more generic types. If the type parameters feature wouldn't exist, the standard library probably wouldn't include the type list, the product integer list, and string list.

[01:32] Since a list is a very common use case, I'm convinced implementing a general-purpose list type accepting a type parameter was a great decision. Another example would be option since some accept all sorts of types. In fact, we can implement our own option like this.

[01:57] Similar to our first example, the type parameter integer is inferred. By the way, type parameters have to start with a tick, and then must be followed by a character or word. In case you wonder, a type can also receive multiple type parameters. Here, we create a record type ship that accepts two type parameters which map to IT and cargo.

[02:26] In addition, types also can be composed. As we see in this example, the type ship has a field of cargo, which is a list of strings. It's also important to mention before we wrap up that using type parameters doesn't mean we lose any type safety guarantees. Data-mining the type is just deferred to a later stage.

~ 4 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today