Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1770

Unnecessarily strong type constraints with polymorphic variants (when using `React.useState`?)

$
0
0

That what you’re perceiving as a problem is exactly by design. Variants (polymorphic or not, I’m using the term loosely in this context) will always require exhaustive checks/pattern matching. Inversely, singular variants will be their own type and will not be interchangeable with each other. i.e type a = [#a] is not the same type as type b = [#b]. That is what your second example defines - the set function inside g() accepts only type [#a].

However, I find this behavior puzzling:
Example 1: this should have been an error, but is not
Example 2: whereas this is an error

I suspect my example 1 and your first example’s f function

let f = v => {
  let x = ref(#b)
  x := v
}

has something to do with block scoping and the rule “The value of the last line of a scope is implicitly returned.” and how the type inference deduces the types in a block scope vs the types in a module. Can someone else explain if this behavior is correct?


Viewing all articles
Browse latest Browse all 1770

Trending Articles