Skip to content

Instantly share code, notes, and snippets.

@ryyppy
Created April 7, 2020 13:54
Show Gist options
  • Save ryyppy/c17ffde71188ee2395527cd3ef8c091e to your computer and use it in GitHub Desktop.
Save ryyppy/c17ffde71188ee2395527cd3ef8c091e to your computer and use it in GitHub Desktop.
Reason's module vs JS Classes
// Example based on following tweet: https://twitter.com/housecor/status/1247497178882486272
// Class Example
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
greet() {
console.log(`Hi ${this.name}`);
}
contact() {
console.log(`Emailing ${this.email}`);
}
}
const cory = new User("Cory", "cory@reactjsconsulting.com");
cory.contact();
// In Reason, code is mainly structured in immutable record types (compiled to JS objects),
// functions and modules... it uses only a few features, but due to functional composition it's very easy
// to build more complex structures (encapsulation etc). Less to learn, more power to the user!
// Playground Link:
// https://reasonml.github.io/en/try?rrjsx=true&reason=LYewJgrgNgpgBAVQM4wE5wLxwN4Cg5wD0AVHAOrxgwBmAlgHYxhwCGcqMAxiKmADRwA7gAtanYQJD04nDiwAutKQMZM48kHGEsAbjHwEWUKK3nzUtAEYR5MJHAAOHFPXkA6D3CS1gtKC1QoAE91TTZOfyR7bnokcwhODXRiQgN5IId4eUwcAwJ6FmAYAC4vcwYAcz48uBhgFj9SuIt6KoMAXwBuAwMSOABRPVQg+VFW2qgUOFp7ACsIONY4agh6RKVpQVpRhjgQCHQAA2Q0Q7hQSFg8gElpahZEgR37LeNWN-MWWP9bUK8fBywZardZSGRGSasejMABSAGU9pZZlx5PZdqN4DBoXAUgZYNkKhwYNksBBMAA+OAwpBuKAgCoACgARAAJWhwJlwADUXLgEDcBSKAEpugQ8cSZFJ5A8SXyKVSaXTGUz+vU-JUOdzefy6g0oCLcF1cLg+gAVYTwOioRYKcrWX7HFCoAWFGBnWzGewYyXABx+NDqbTZQTwALwbTQqAatjpTJ5LY7aTek7oC7QeBIbiZHGpfGS4Y5PAEFMuoqlJkAYR4QSZ1QIusaHO4wwAAnJErNM1IkNBFK03NxgEzDd0TaR+gBHCC0HRGLHZDSIJ0DqUyhnNoJCjxuWpDEZjCrTOYLbJsFZrRRg3EbgC05JLMWliU6QA
module User = {
// We defined a record, which, on creation, need to have
// all attributes present... similarly to a class constructor
type t = {
name: string,
email: string,
};
// Everything else is just a function within our `User` module
// In fact, this will all translate to simple function calls and JS objects in the end
let greet = u => Js.log("Hi " ++ u.name);
let contact = u => Js.log("Emailing " ++ u.email);
};
// The first attribute `User.name` tells the compiler that we are handling a type
// within the User module scope
let cory = {
User.name: "Cory",
email: "cory@reactjsconsulting.com"
};
// Equivalent to User.contact(cory)... everything is just a function
cory->User.contact;
// This is the compiled JS output for the `module-example` file
// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE
'use strict';
function greet(u) {
console.log("Hi " + u.name);
return /* () */0;
}
function contact(u) {
console.log("Emailing " + u.email);
return /* () */0;
}
var User = {
greet: greet,
contact: contact
};
var cory = {
name: "Cory",
email: "cory@reactjsconsulting.com"
};
contact(cory);
exports.User = User;
exports.cory = cory;
/* Not a pure module */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment