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?
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.
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"
}
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:
Hi, any way to get a list of type name?
type result =
| Ok
| Error
should return string array:
["Ok", "Error"]
I don’t think you can generate this automatically, but I’d say it’s not much of a big deal to write it yourself.
There’s a technique that allows you to make sure you didn’t forget a case in your array:
module Foo: {
type t = Bar | Baz
let elements: array<t>
} = {
type t = Bar | Baz
type dup = Bar | Baz // warning : unused constructor Baz.
let elements = ([Bar] :> array<t>)
}
This way, when you add new cases in your variant, the compiler will remind you to add it to your array too.
@tsnobip Hi, thanks for your quick response. why we need to define module here?
If I remove module
module Foo: {
type t = Bar | Baz
let elements: array<t>
} = {
the warn was gone.
And if some type like
type result =
| Ok
| Error
| ErrCode(int)
I need to extract the TAG name by some raw JS code?
The module type is here to make the dup
type internal only, given it can’t be used outside of the module, the compiler can track if all of its variants are used or not.
Can I ask you why you need to get the TAG name? If you need to convert each variant to a string, I’d rather do it manually than making it depend on some internals.
Thanks for your quick response.
I am learning redux toolkit, and try to make it work on ReScript + Preact.
The reducers need some unique string for the action name (example code, so I think the type name is quite suitable for this case.
Here is my sample code, and I need to define action names manually now
to be honest, given rescript has native support for a tool as powerful as variants/algebraic data types and exhaustive pattern matching, basic useState
and useReducer
can take you very far away, I’d stick to it for now if I were you, I think redux usage in rescript is pretty low because of this.
Thanks for your advice, while application comes too big, redux (toolkit) is a good tool.
I find your example can write more simple (without define twice type myType
)