May 7, 2021

ReScript 9.1

Featuring a new npm package, a CLI revamp, polymorphic variant interop and object cleanup.

ReScript Team
Core Development

Exciting Improvements in ReScript 9.1

Our recent few releases of ReScript contains lots of improvements, among which are a few standout features we'd like to further promote. Hope you're as excited as we are about these! It goes without saying, our updated editor plugin works with the new releases.

New NPM Package

We've finally moved from bs-platform to rescript!

This is mostly just a long overdue name change; the package's virtually identical. Apart from the renaming of our CLI.

CLI Cleanup

We took the occasion of the NPM package move to also unify the binaries bsc, bsb and their various commands into a single rescript command:

SH
❯ rescript -help Available flags -v, -version display version number -h, -help display help Subcommands: build clean format convert help Run rescript subcommand -h for more details, For example: rescript build -h rescript format -h The default `rescript` is equivalent to `rescript build` subcommand

Here's a table of translation, if you're upgrading your script that is currently using bsc and bsb:

  • bsc -format myFile.res: rescript format myFile.res

  • bsb: rescript build *

  • bsb -make-world: rescript build -with-deps *

  • bsb -w: rescript build -w

  • bsb -w -make-world: rescript build -w -with-deps *

* However, we've gone even further to improve your experience; in most cases you won't need to invoke build, nor -with-deps anymore! Not only is rescript an alias to rescript build, it also smartly detects whether your dependencies are already built; if not, it builds them automatically.

This means that you can ditch your old -make-world (now the explicit -with-deps flag, for edge-case explicit usages). Just call rescript and everything including dependencies will always be built! As performance is our highest priority, we've ensured that such extra detections does not slow down the build.

Polymorphic Variants for Numbers and Strings

Drumrolls

  • Poly variants like #1, #42 compile to JavaScript numbers.

  • Poly variants like #hello, #world compile to JavaScript Strings.

This is a feature many of you were probably waiting for. Now you can interop with a JavaScript value that's a limited set of numbers or strings:

ReScriptJS Output
let secret = #42

// optional type annotation, for documentation
type t = [#1 | #3 | #5 ]

// enjoy the pattern matching
let test = (arg: t) => {
  switch arg {
  | #1 | #3 => "hello"
  | #5 => "world"
  }
}

But wait, there's more. We allow safely coercing these poly variants to ints or strings even at the ReScript side:

ReScriptJS Output
let test2 = (arg: [#1 | #3 | #5]) => {
  (arg :> int)
}

let test3 = (arg: option<[#1 | #3 | #5]>) => {
  (arg :> option<int>)
}

Js.log(test2(#1))
Js.log(test3(Some(#3)))

As usual, check the output tabs: there's no runtime cost. Time to upgrade some interop!

Object Cleanup

Our objects had various constraints due to legacy reasons; we've managed to clean them up, and expose the UX that they deserve:

  • Js.t<{"x": int}> is now simply {"x": int}. Existing code using Js.t still work; it's now a no-op. Our rescript format will also format them away.

  • You can now use object type spread:

    type point2d = { "x": float, "y": float, } type point3d = { ...point2d, "z": float, }

The cleanup also allowed us to unlock very exciting ideas. For example, this one.

What's Next?

First class unicode support! Expect being able to write the following:

RES
let helloUnicode = (x) =>{ switch x { | '❤️' => "ReScript is awesome" | 'Σ' => "Math is fun" | _ => "Lots of unicode" } }

Conclusion

Don't miss our various other improvements in our changelog. As always we try to keep our changes performant, lean and robust. We hope you'll enjoy these.

See you next time!

Want to read more?
Back to Overview