You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order to compile a polymorphic variant to a desired string value in JavaScript, the following approach is required:
type x =
[ `Document_fonts [@bs.as "document-fonts"]
| `Browser_fonts [@bs.as "browser-fonts"]
]
[@@bs.deriving jsConverter]
let a : x = `Document_fonts
let b = xToJs `Document_fonts
The assignments are compiled to
var a = "Document_fonts";
var b = xToJs("Document_fonts");
out of which, b is correctly assigned the value "document-fonts".
However, [@@bs.deriving jsConverter] generates converter functions, which are also compiled and added to the JavaScript code. So the full compiled code is quite a bit:
var _map = {"Document_fonts":"document-fonts","Browser_fonts":"browser-fonts"};
var _revMap = {"document-fonts":"Document_fonts","browser-fonts":"Browser_fonts"};
function xToJs(param) {
return _map[param];
}
function xFromJs(param) {
return _revMap[param];
}
var b = xToJs("Document_fonts");
var a = "Document_fonts";
export {
xToJs ,
xFromJs ,
a ,
b ,
}
Things could be a lot cleaner:
The requirement of [@@bs.deriving jsConverter] attribute could be dropped, i.e., the definition of the polymorphic variant type, coupled with the [@bs.as "x"] attribute(s) should provide the intent adequately.
Ideally, i.e., from my understanding, the converter functions should not be needed and could be dropped too.
An assignment like
let a : x = `Document_fonts
for the above definition would ideally compile directly to
var a = "document-fonts";
The text was updated successfully, but these errors were encountered:
This isn't actually possible because we'd need to support defining polymorphic variants that break the OCaml syntax. i.e. it's not possible to write type x = [`foo-bar]
In order to compile a polymorphic variant to a desired string value in JavaScript, the following approach is required:
The assignments are compiled to
out of which,
b
is correctly assigned the value"document-fonts"
.However,
[@@bs.deriving jsConverter]
generates converter functions, which are also compiled and added to the JavaScript code. So the full compiled code is quite a bit:Things could be a lot cleaner:
[@@bs.deriving jsConverter]
attribute could be dropped, i.e., the definition of the polymorphic variant type, coupled with the[@bs.as "x"]
attribute(s) should provide the intent adequately.for the above definition would ideally compile directly to
The text was updated successfully, but these errors were encountered: