-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to not include undefined values in resulting object literal #801
Comments
What would you expect the following to do ? let f x =
object%js (self)
val z = x [@@jsoo.optdef]
end |
@hhugo I don't think the current behavior of So in the snippet you shared, calling |
In case it helps, this is the part of the BuckleScript codebase where the Here's the documentation for it. As you can see, it walks through the args of the external declaration, and then calls I know this kind of design with annotations for externals is not common in jsoo... but I thought it'd be worth mentioning for more context. |
I'm not against the idea. What do you expect the generated javascript to be. Creating an empty object and and have a bunch of if-then to set fields if not undefined? |
This is a sample implementation I did using the existing APIs: let optInj prop opt =
match opt with
| Some (s) -> [|(prop, Js.Unsafe.inject s)|]
| None -> [||]
let create ?x:(x : string option) ?y:(y : int option) ?z:(z : int option) () =
optInj "x" x
|> Array.append (optInj "y" y)
|> Array.append (optInj "z" z)
let obj = Js.Unsafe.obj (create ~x:"2" ~y:2 ()) (* {x: "2", y: 2} *)
let obj2 = Js.Unsafe.obj (create ()) (* {} *) |
@hhugo Yes, in the same vein as this. As a side question, I'm not sure how BuckleScript does it but it inlines most of the usages. I guess this is an optimization that already exists in the OCaml compiler, right? |
Was there any action taken as a result of this question? I would find that capability quite useful. |
Using
It still includes
{z: undefined}
in the resulting object literal. It'd be helpful to have an option (or a new attribute) to just not include the property altogether if it's undefined.BuckleScript has something similar with
bs.obj
(example).cc @Drup
The text was updated successfully, but these errors were encountered: