Explicit polymorphic type constraints

I was reading through this wonderful blog post by @_craigfe on Polymorphic type constraints and learned about how to explicitly define a polymorphic function using an explicit type signature.

ReasonML syntax:

let id: 'a. 'a => 'a = x => x;

I tried a few variations, but I wasn’t able to find a syntax that worked in the ReScript playground. Does this feature exist in ReScript?

1 Like

I apparently didn’t try the most obvious one: the same syntax.

This works!

let id: 'a. 'a => 'a = x => x;
4 Likes

Thank’s for sharing these articles and show that is also possible on rescript (obviously ? but still :slight_smile: )

1 Like

Just fyi. In (I’d say) most cases you don’t need explicit polymorphic types as long as you use an interface file (or module type), because that also forces the generic types to be fully polymorphic:

module Id: {
  let id: 'a => 'a
} = {
  let id = x => x // Replace with x => 0 to get an error
}
4 Likes