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

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

$
0
0

Thank you for the thorough reply, I really appreciate it!

However I am still puzzled by:
" * there isn’t any possibility of x being something else than #a"
If I slightly modify g:

let g = () => {
  let (x, _setX) = State.use(() => #a) // x is infered as: [> #a]
  switch x {
  | _ => f2(#a)
  }
}

x is now infered as [> #a]. I suspect the exhaustiveness check of the switch cases forces x to have a closed type [ #a ]; I could reproduce the same behavior with an OCaml version of this code. I’m not really sure what this “feature” is trying to prevent.

My use case is… again with GraphQL, where my React component only uses [ #asc | #desc ], while the ppx-generated MyQuery.use({orderBy: orderBy}) expects me to pass

[
  | #desc_nulls_last
  | #desc_nulls_first
  | #desc
  | #asc_nulls_first
  | #asc_nulls_last
  | #asc
]

So probably it is best to alias the above lengthy type to something like order and use :> order.
I’m not completely satisfied with this solution, as the use of the GraphQL API in a separate function leaks the implementation detail, contaminating other functions.


Viewing all articles
Browse latest Browse all 1948

Trending Articles