Hi there !
I have written a small library that I want to publish without any external dependencies.
The only dependency left, is this:
var p = proxied[extra$1];
if (p !== undefined) {
return Caml_option.valFromOption(p);
}
I know that the “p” is not an option at this stage and the valFromOption just returns p.
How can I avoid the library dependency to Caml_option and simply return “p” (without altering the sources) ?
The ReScript code is:
switch Dict.get(observed, key) {
| Some(eyes) =>
if Set.delete(eyes, sym) {
if Set.size(eyes) == 0 {
ignore(%raw(`delete observed[key]`))
}
}
| _ => ()
}
gaspard
December 7, 2024, 10:10pm
2
I rewrote the code with a version of Dict that uses Reflect and check for existing key with Reflect.has.
Still curious if there are any tricks for my weird use case (create a TypeScript and ReScript library without any external dependencies).
It’s something that can hopefully be improved in the future, but for now the recommended approach is to use a bundler before publishing.
fham
December 8, 2024, 2:02pm
4
One thing you can do is to create your own Dict bindings that return nullable instead of Option.
module Dict = {
type t<'a> = dict<'a>
@get_index external get: (dict<'a>, string) => nullable<'a> = ""
@val
external fromArray: array<(string, 'a)> => dict<'a> = "Object.fromEntries"
}
let key = "a"
let observed = [(key, Set.make())]->Dict.fromArray
let sym = "x"
switch Dict.get(observed, key) {
| Value(eyes) =>
if Set.delete(eyes, sym) {
if Set.size(eyes) == 0 {
ignore(%raw(`delete observed[key]`))
}
}
| _ => ()
}
Playground link
Excellent !
Thank you very much
On the side note, I wrote this little library in TS first and wanted to rewrite in ReScript to see if it helps (I have many years of OCaml coding and am doing TS for work a lot).
The experience proved very fruitful. The finished library is much simpler, less “bent” in weird directions, with a cleaner api and should even be faster and easier to reason about.
I cannot really tell if it is the fact of rewriting or the language but my guess is that the biggest impact comes from the language. I feel that some languages push my brain to more or less insanity.
5 Likes
fham
December 8, 2024, 7:23pm
6
I want to frame that post, thanks for the feedback!
2 Likes