Skip to content

An optional field of an @obj external is evaluated after the fields that follow it #9225

Closed

Description

@mununki

An optional labeled argument of an @obj external whose presence is not known at compile time is evaluated after all the other fields, so the arguments do not run in source order:

let log = []
let size = () => {
  log->Array.push("size")
  Some(1)
}
let mode = () => {
  log->Array.push("mode")
  "dark"
}
@obj external make: (~size: int=?, ~mode: string, unit) => {..} = ""
let o = make(~size=?size(), ~mode=mode(), ())
let tmp = {
  mode: mode()
};

let tmp$1 = size();

if (tmp$1 !== undefined) {
  tmp.size = tmp$1;
}

let o = tmp;

log is ["mode", "size"].

Lam_compile_external_obj.assemble_obj_args (compiler/core/lam_compile_external_obj.ml) puts an optional argument whose presence is not known into a list of assignments and builds the object literal from the other fields first. Each deferred argument is then bound to a variable by Js_ast_util.named_expression and assigned under an if, after every field of the literal has been evaluated. A comment in the same function says that the evaluation order is undefined.

Since #9217, a polymorphic variant field gets a temporary before the object is built, so make(~level=?level(), ~mode=mode(), ()) with ~level: @int [#low | #high]=? runs in order. Other optional fields do not.

Reproduced with 12.3.1 and master (fc74442).

Related: #9175, #9215, #9217.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions