Is there a way to get the name of variant in type

Is there a method to return the name instead of a number for the tag?

For example:

type result =
  | Ok
  | Error

Given a variable of type result, is there a way to print “Ok” or “Error” instead of “0” or “1” like it does by default?

Just tried it with rescript v. 11 (currently in alpha stage). It will print “Ok” and “Error” instead of 0 and 1.

3 Likes

polymorphic variants compile to more JS-friendly values

in

type result = [#ok | #error]

#ok compiles to “ok” and #error compiles to “error”

but if you wanna stick with regular variants you could also just write a function that converts them like

let result_to_string = result => switch result {
| Ok => "Ok"
| Error => "Error"
}
2 Likes

That’s nice, I tried and it worked a treat.
Where can I report bugs in ReScript 11 alpha 6?

AFAIK Rescript compilers’ GitHub repository should be used to report any issue regarding rescript: