Search code examples
modulerescript

How to write short local open in ReScript?


This compiles in ReasonML:

let testFn = who => Js.(log("Hello " ++ who ++ "!"));

but not in ReScript:

FAILED: src/test.ast

  Syntax error!
  /xxx/src/test.res:1:25-27

  1 │ let testFn = who => Js.(log("Hello " ++ who ++ "!"));
  2 │

  I'm not sure what to parse here when looking at "(".


  Syntax error!
  /xxx/src/test.res:1:25-27

  1 │ let testFn = who => Js.(log("Hello " ++ who ++ "!"));
  2 │

  consecutive statements on a line must be separated by ';' or a newline

I didn't find any mention of removal in official docs. Did I miss it? Has syntax changed, or was it removed and not mentioned in docs?


Solution

  • As pointed out by @Yawar in the comments, this short-hand is not supported at time of writing, but is likely to be at some point in the future (see https://github.com/rescript-lang/syntax/issues/2 for discussion).

    And just to save a click for those coming across this, a workaround is to rewrite it using a local scope and opening the module in that scope:

    let testFn = who => {
      open Js
      log("Hello " ++ who ++ "!")
    }