Skip to content

Instantly share code, notes, and snippets.

@ENAML
Created September 29, 2018 20:12
Show Gist options
  • Save ENAML/bb4044153638e16cef0b0bdcaf66a637 to your computer and use it in GitHub Desktop.
Save ENAML/bb4044153638e16cef0b0bdcaf66a637 to your computer and use it in GitHub Desktop.
ReasonML class example
/**
* Class test:
* ---------------
* (see: https://v1.realworldocaml.org/v1/en/html/classes.html)
*/
class stack ('a) (init) = {
as _self;
val mutable v: list('a) = init;
pub pop = () => {
switch (v) {
| [hd, ...tl] =>
v = tl;
Some(hd);
| [] => None
};
};
pub push = (hd) => v = [hd, ...v];
pub iter = (f) => List.iter(f, v);
pub log = () => {
Js.log2("stack: ", v);
};
};
let run = () => {
let s = (new stack)([]);
for (i in 0 to 5) {
s#push(i);
};
let myIterFn = (elt)=> Js.log2("elt: ", elt);
s#iter(myIterFn);
s#log();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment