Polymorphic variant compile error - confused

I don’t understand why this won’t compile.

  type vowels = [#a | #e | #i | #o | #u]
  type consonants = [#b | #c | #x]
  type letters = [vowels | consonants]
  let test = () => {
    let letterA: vowels = #a
    let assignmentTest1: letters = #a
    let assignmentTest2: letters = letterA // compile error
  }

The error is…

This has type: vowels
Somewhere wanted: letters
The first variant type does not allow tag(s) b, c, `x

===
My use case is I’ve got an external function that takes something like a letters parameter. I’m trying to invoke this function with an instance of a vowel and can’t get it to compile.

You can use the :> type coercion operator:

let assignmentTest2: letters = (letterA :> letters)

Note that if you remove the type annotations from the values, then it will also compile (since the compiler infers the broadest possible types for each value).

2 Likes