Casting polyvariant tail?

If i have two poly variants, with one extending another.
And I make a switch statement that captures the outer value
is there a natural way to pass the tail into a function that takes type inner?
The compiler complains but it seems like it should be inferred?

https://rescript-lang.org/try?version=v10.1.2&code=C4TwDgpgBAlgdnCAnKBeKBtAxAQQBTzACUUAPlFgEIFzEC6AUADYTBQBmc8iK6eAHgC5YCZCVQA+KAG8GUKAGcA7jGABjABZR+MuWQr4Y4qTD3kqBY7CgAqWHoC+DJ6EhQA9gFdgyNJiwAwvrcyIzMrBxwXj68UALC0WJoUrLyyqqa2rry5kGSUADMZln5-AC0EpwhSI7ODEA

Yes you can do that. Use the type definition as an abbreviation in the pattern.

See playground

type inner = [#A(int) | #B(int)]

let fninner = (x: inner) => {
  switch x {
  | #A(i) => i
  | #B(i) => i * i
  }
}

type outer = [#C | inner]

let fnouter = (x: outer) => {
  switch x {
  | #C => 3
  | #...inner as x => x->fninner
  }
}

Edit: here is section from manual about this syntax

Edit 2: Fixed the link to the playground (originally it was to the OP’s playground)

4 Likes

Ahh there it is.
Thanks Ryan!

1 Like