Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
800 views
in Technique[技术] by (71.8m points)

module - 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?

question from:https://stackoverflow.com/questions/65909442/how-to-write-short-local-open-in-rescript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ++ "!")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...