A drop-in standard library for ReScript. Intended to be familiar for JavaScript developers, easy to use, and be rich enough (without being bloated) so that you don't need to reach for anything else for typical ReScript development.
We're proposing a new, experimental drop-in standard library intended to solve a number of problems we see in ReScript development today. This proposed standard library has the following purposes:
- Adding a rich (enough) standard library that's familiar to JavaScript developers
- Clearing up the current confusing situation with regards to t-first vs t-last, OCaml pervasives,
Js.Array2etc - Prepare for "uncurried by default" by only shipping compatible APIs
In practice, the proposed standard library is a fusion of rescript-js, rescript-promise, and Belt - all battle tested and well liked, with a few minor additions sprinkled in. So while we're calling this a new standard library, the parts it's made up of isn't new.
It ships as a separate package, so you can try it out and start migrating to it if you want at your convenience. Eventually, if the community likes it and it stands the test of time, it might make its way into the compiler. And, at that point it would replace the current Js namespace.
You're encouraged to start using this today. We don't anticipate nor plan for larger changes to any of the APIs, but please be aware that changes may still happen as this project stabilizes. We're of course also interested in the community's feedback on the APIs. The DX of using Core is going to gradually improve as we add doc strings, documentation and so on.
We've been following a few guiding principles as we developed this:
- Familiarity for JavaScript developers. Let APIs follow their JS equivalents as much as possible, and where there are new APIs that don't exist in the JS standard library, follow a naming and layout that we think will feel familiar to a JS developer. Example:
Belt.Array.keepMapis included in theArraymodule, but here renamed toArray.filterMapto be more familiar to JS developers. - Focus on zero cost APIs, but prioritize quality. Sometimes a small bit of runtime is required for the API to be as good as it can be.
- Effortless inference is important here. Choose APIs where inference just works. Example:
Belt.Array.reduceis included instead ofJs.Array2.reduce, because theBeltversion's inference is better than theJs.Array2version.
- @bloodyowl + contributors to
rescript-jsare the people who've done the heavy lifting here, since this stdlib is based heavily onrescript-js. - Also a shout out to the authors of
Belt, as a few key things have been incorporated directly fromBelt. - Patrick (@ryyppy) for his work on
rescript-promise, which is fully inlined into the stdlib.
ReScript >=10.1 is required.
$ npm install @rescript/coreThen add @rescript/core to your bsconfig.json's bs-dependencies:
{
"bs-dependencies": [
+ "@rescript/core"
]
}Open it so it's available in the global scope.
{
"bsc-flags": [
+ "-open RescriptCore",
]
}All relevant standard library modules are designed to be available directly in the global scope, just like you expect them to.
Console.log("Hello world!")
let timeout = setTimeout(() => {
Console.log("Hello!")
}, 100)
clearTimeout(timeout)
let array = [1, 2, 3]
let sum = array
->Array.map(x => x * 2)
->Array.reduce(0, (acc, item) => acc + item)
let maybeValidFloats = ["1", "1.5", "some random string"]
let validFloats = maybeValidFloats
->Array.filterMap(v => v->Float.fromString)Documentation will be added successively to this repository. We're looking for help in producing good, high quality docstrings for all APIs. More information coming on how you can help out with this.
During the transition phase to this standard library you might find yourself needing to access the current global Array/List etc modules that originate from OCaml. These will be removed eventually, but in the transition phase you'll be able to access them by adding this open at the top of any file:
open OCamlCompatThis standard library is based on rescript-js, but with the tweaks and modifications. Some of the changes are listed below. There are also no uncurried versions of anything in the standard library. This is because uncurried by default will be coming soon in ReScript 11, which will remove the need for explicitly uncurried functions.
reduce/reduceReverseand friends (withIndex versions) are taken fromBeltand replace the bindings to the JavaScript equivalents (reduceandreduceRight). Thereduceversions fromBeltworks fully with type inference because of the argument order being reversed (initvalue comes first), whereas the JavaScript versions don't work well with inference. The runtime added for this is minor (and very fast still), and we want users to have to annotate as little as possible for the standard functions they'll be using.push/pushMany/unshift/unshiftManyare changed to returnunit, for convenience. In JS, these return the new length of the array. That's however extremely rare to actually use, and you can just doArray.length(array)after pushing to get the new length. Changing the return type to beunitgets rid of needing to dolet _ =(or->ignore), which can be confusing for beginners.findIndexOpt/lastIndexOf/indexOfOptare added, returningNoneinstead of-1if the item searched for does not exist. These are in addition tofindIndex/lastIndexOf, which still returns-1when the item you're looking for does not exist.getUnsafeadded (copied fromBelt).setUnsafeadded (copied fromBelt).reverseadded (copied fromBelt), in addition to existingreverseInPlace.reverseInPlaceis zero cost but does not produce a new array.reversedoes produce a new array.keepMapis added fromBelt, but renamed tofilterMap. Rationale:filterMapis closer to the JS convention of naming. It's also available in other languages like Rust.keepet al can confuse beginners, who're bound to be looking forfilterstyle names since that's what JS has.shuffleandshuffleInPlaceare added (copied fromBelt).flatMapadded (copied fromBelt, but using nativemapandconcatfunctions).
fromStringis copied fromBelt. Main difference is thatfromStringnow returns anoptionthat'sNoneif the parsed float isNaN. If you want the raw JS behavior of potentially parsing a float toNaNyou can useFloat.parseFloat(string).
searchOpt/indexOfOpt/lastIndexOfOptadded. Convenience methods for returning anoptioninstead of returning-1for not found.- Added bindings for
localeCompare.
The Promise module is inlined from https://github.com/ryyppy/rescript-promise, with these additions:
- Jaap's
ignorePromisePR is merged.
- The above stated modules are brought in from
Belt, since they're widely used in the ecosystem. - In
OptionandList, the same naming convention is applied as inArrayforkeep*functions. As inkeepbecomesfilter,keepMapbecomesfilterMap, etc.
windowanddocumentare typed asDom.window/Dom.documentrather than open objects ({..}).
Things are added to this section on migration gradually.
Migrating to the new standard library should be easy to do gradually. In this section we'll gather information that's intended to help migrating as painlessly as possible.
In general, we suggest you take the following gradual approach:
- Install the stdlib and open it in the global scope
- Fix any inconsistencies that uncovers. That will be things like module name clashes (more details below),
someArr[0]now returning an option, and so on. - Run the semi-automated migration below. This will uncover more things to fix.
- Do a final search for
Js.in your project, and replace any leftovers that the migration script did not catch.
Check out this issue on the GitHub tracker if you get stuck. We'll do what we can to help out when migrating there.
We've prepared a script you can run with comby.dev that will do parts of the migration for you automatically. The migration script is located in migration.toml. Here's an example of how you can run it:
# Run in your project root. Assumes `migration.toml` has been copied in place to your project root.
comby -config migration.toml -f .res -matcher .re -exclude-dir node_modules,__generated__ -i
# You should run it twice - once for .res files, and once for .resi. The command below runs for .resi, and the one above for .res.
comby -config migration.toml -f .resi -matcher .re -exclude-dir node_modules,__generated__ -iThe migration script is a set of instructions that Comby runs in sequence. You're encouraged to take migration.toml and tweak it so it fits your needs. Comby is powerful. It can do interactive rewriting and numerous other useful stuff. Check it out, but please note it's not intended to cover all of the migration necessary. You'll still likely need to do a few manual fixes after running the migration scripts.
Since the standard library is designed to live in the global scope, you might have your own modules or modules from one of your dependencies whose names collide with the modules from the standard library.
This is a side effect of shipping the standard library as its own package, meaning it won't be a problem if the standard library is adopted and ships with the compiler. At that point you'll be able to have your own modules shadowing builtin modules.
In the meantime, as a workaround:
- For a conflict with your own module, the easiest way to solve this is to just rename your own module to something else.
- For a conflict with a dependency - e.g., Core's
Intlvs. rescript-react-intl'sIntl, you could use patch-package to rename the conflicting module in Core (or remove it altogether if you are not using it).