Skip to content

Instantly share code, notes, and snippets.

@linktohack
Last active June 29, 2018 09:30
Show Gist options
  • Save linktohack/3469de4b6ac6925e2e41198cb5460215 to your computer and use it in GitHub Desktop.
Save linktohack/3469de4b6ac6925e2e41198cb5460215 to your computer and use it in GitHub Desktop.
ReasonML inheritance interop
// Generated by BUCKLESCRIPT VERSION 3.1.5, PLEASE EDIT WITH CARE
'use strict';
class BaseModel {
static findById() {
return new this();
}
updateAttribute(k, v) {
this[k] = v;
return this;
}
toJSON() {
return this;
}
}
class Todo extends BaseModel {
constructor(props) {
let {title, completed} = props || {};
super();
this.title = title || "";
this.completed = completed || false;
}
}
let todo1 = Todo.findById().updateAttribute("title", "Done");
console.log('todo1', todo1);
;
var Instance = /* module */[];
var Model = /* module */[/* Instance */Instance];
var todo2 = Todo.findById(1).updateAttribute("title", "Done");
console.log("todo2", todo2);
console.log(todo2.toJSON().completed);
exports.Model = Model;
exports.todo2 = todo2;
/* Not a pure module */
%bs.raw
{|
class BaseModel {
static findById() {
return new this();
}
updateAttribute(k, v) {
this[k] = v;
return this;
}
toJSON() {
return this;
}
}
class Todo extends BaseModel {
constructor(props) {
let {title, completed} = props || {};
super();
this.title = title || "";
this.completed = completed || false;
}
}
let todo1 = Todo.findById().updateAttribute("title", "Done");
console.log('todo1', todo1);
|};
module Model = {
module Instance = {
type t('a);
[@bs.send.pipe: t('a)] external toJSON : unit => 'a = "toJSON";
[@bs.send.pipe: t('a)]
external updateAttribute : (string, 'b) => t('a) = "updateAttribute";
};
type t('a);
[@bs.send.pipe: t('a)]
external findById : int => Instance.t('a) = "findById";
};
type todoAttributes = {
.
"title": string,
"completed": bool,
};
[@bs.val] external todoModel : Model.t(todoAttributes) = "Todo";
let todo2 =
todoModel
|> Model.findById(1)
|> Model.Instance.updateAttribute("title", "Done");
Js.log2("todo2", todo2);
Js.log((todo2 |> Model.Instance.toJSON())##completed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment