Introducing ReScript: Functional Programming for Web Applications 1st Edition Danny Yang download
Introducing ReScript: Functional Programming for Web Applications 1st Edition Danny Yang download
https://ebookmeta.com/product/introducing-rescript-functional-
programming-for-web-applications-1st-edition-danny-yang/
https://ebookmeta.com/product/learn-clojurescript-functional-
programming-for-the-web-1st-edition-andrew-meredith/
https://ebookmeta.com/product/introducing-functional-programming-
using-c-leveraging-a-new-perspective-for-oop-developers-1st-
edition-vaskaran-sarcar/
https://ebookmeta.com/product/functional-programming-for-
dummies-1st-edition-john-paul-mueller/
https://ebookmeta.com/product/artificial-intelligence-basics-a-
non-technical-introduction-1st-edition-tom-taulli/
Graphene and its Derivatives Volume 2 Water Wastewater
Treatment and Other Environmental Applications
Kaustubha Mohanty
https://ebookmeta.com/product/graphene-and-its-derivatives-
volume-2-water-wastewater-treatment-and-other-environmental-
applications-kaustubha-mohanty/
https://ebookmeta.com/product/linux-hardening-in-hostile-
networks-server-security-from-tls-to-tor-kyle-rankin/
https://ebookmeta.com/product/who-goes-to-school-margaret-
hillert/
https://ebookmeta.com/product/consumers-and-food-understanding-
and-shaping-consumer-behaviour-1st-edition-professor-marian-
garcia-martinez/
https://ebookmeta.com/product/peril-on-the-page-1st-edition-
margaret-loudon/
Against the Clock Dark Tide Mysteries and Thrillers 8
1st Edition Mark Allan Gunnells Shane Nelson Brandon
Ford
https://ebookmeta.com/product/against-the-clock-dark-tide-
mysteries-and-thrillers-8-1st-edition-mark-allan-gunnells-shane-
nelson-brandon-ford/
Danny Yang
Introducing ReScript
Functional Programming for Web Applications
Danny Yang
Mountain View, CA, USA
This work is subject to copyright. All rights are solely and exclusively
licensed by the Publisher, whether the whole or part of the material is
concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in
any other physical way, and transmission or information storage and
retrieval, electronic adaptation, computer software, or by similar or
dissimilar methodology now known or hereafter developed.
The publisher, the authors, and the editors are safe to assume that the
advice and information in this book are believed to be true and accurate
at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the
material contained herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
History of ReScript
The lineage of ReScript can ultimately be traced back to the ML family
of languages originating from the 1960s. In particular, ReScript is
directly based on OCaml, a general-purpose programming language
that was developed in the 1980s and used today for systems
programming in academia and industry.
In 2015, Jordan Walke, the creator of the React web framework,
developed a toolchain and alternative syntax for OCaml called Reason.
Reason was designed to bridge the gap between the web and OCaml
ecosystems – it could be compiled into both native machine code and
JavaScript, allowing web developers to take advantage of OCaml’s
features. Static typechecking and OCaml’s sound type system
eliminated many common bugs in JavaScript code, and OCaml’s
immutability and functional style was a great fit for React.
Reason was compiled to JavaScript using a compiler called
BuckleScript, which was developed at Bloomberg around the same time
Reason was being created at Facebook.
Around 2020, the BuckleScript project created a new language
based on Reason that could only be compiled to JavaScript using the
BuckleScript compiler, and so ReScript was born.
ReScript has the following key differences from its predecessors:
ReScript has different syntax and features. While it looks and feels
more like JavaScript, ReScript is still based on the battle-tested
compiler and type system as Reason and OCaml, so it has the same
type safety benefits as its predecessors.
ReScript can only be compiled to JavaScript. By dropping support
for native compilation, ReScript has a simpler toolchain and standard
library, along with a feature set better suited for web development.
This makes ReScript easier for newcomers to learn and allows for
smoother integration with other web technologies.
Installing ReScript
ReScript projects are set up just like modern JavaScript projects, except
we have an extra development dependency on the rescript package.
Before we begin, make sure you have Node.js v10 or higher, and
npm. To check the version of Node.js you have, run node -v in your
terminal. If you don’t have Node.js installed, you can find installation
instructions at https://nodejs.org.
Once you’ve confirmed that you have the right version of Node.js
installed, create a new directory for your project, and run npm
install rescript@10 to install v10 of ReScript.
There will be a package.json file with the following contents
inside the project’s root directory. Add the following scripts to it:
{
...
"scripts": {
"build": "rescript",
"start": "rescript build -w"
}
}
The scripts we added in package.json are used to run the
ReScript compiler to compile our ReScript to JavaScript.
npm run build will compile all the ReScript files in the project.
npm run start will start a process that watches the project and
automatically recompiles whenever anything changes.
Next, create a bsconfig.json file inside the same directory, with
the following contents:
{
"name": "your-project-name",
"sources": [
{
"dir": "src",
"subdirs": true
}
],
"package-specs": [
{
"module": "commonjs",
"in-source": true
}
],
"suffix": ".bs.js",
"bs-dependencies": []
}
The dir field specifies which directory ReScript source files are
located, in this case under the folder src. For every ReScript file
Foo.res under src, the compiler will output a JavaScript file named
Foo.bs.js in the same location as the original source.
Now we’re ready to write some ReScript!
Js.log("hello, world")
console.log("hello, world");