Skip to content

Instantly share code, notes, and snippets.

@zerobias
Created October 15, 2018 08:18
Show Gist options
  • Save zerobias/a1a7065894287c3ade0d9f52ab271c40 to your computer and use it in GitHub Desktop.
Save zerobias/a1a7065894287c3ade0d9f52ab271c40 to your computer and use it in GitHub Desktop.
module type DEVICE = {
let draw: string => unit;
};
let devices: Hashtbl.t(string, module DEVICE) = Hashtbl.create(17);
module SVG = {
let draw = string => Js.log2("svg module", string);
};
Hashtbl.add(devices, "SVG", (module SVG): (module DEVICE));
module PDF = {
let draw = string => Js.log2("pdf module", string);
};
Hashtbl.add(devices, "PDF", (module PDF): (module DEVICE));
exception E;
module Device = (
val try (Hashtbl.find(devices, "PDF")) {
| Not_found =>
Js.log("Unknown device %s\n");
raise(E);
}:
DEVICE
);
let draw_using_device = (device_name, picture) => {
module Device = (val Hashtbl.find(devices, device_name): DEVICE);
Device.draw(picture);
};
Js.log(devices);
draw_using_device("PDF", "+_+")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment