Skip to content

Instantly share code, notes, and snippets.

@idkjs
Created June 27, 2021 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idkjs/2c21d26248d4e398928642e6bac5b9d4 to your computer and use it in GitHub Desktop.
Save idkjs/2c21d26248d4e398928642e6bac5b9d4 to your computer and use it in GitHub Desktop.
try a function example
let try_finally = (~always, f) =>
switch (f()) {
| x =>
always();
x;
| exception e =>
always();
raise(e);
};
let use_output = command => {
let fn = Filename.temp_file("ocaml", "_toploop.ml");
try_finally(
~always=
() =>
try (Sys.remove(fn)) {
| Sys_error(_) => ()
},
() =>
switch (
Printf.ksprintf(Sys.command, "%s > %s", command, Filename.quote(fn))
) {
| 0 => ignore(Toploop.use_file(Format.std_formatter, fn): bool)
| n => Format.printf("Command exited with code %d.@.", n)
},
);
};
let () = {
let name = "use_output";
if (!Hashtbl.mem(Toploop.directive_table, name)) {
Hashtbl.add(
Toploop.directive_table,
name,
Toploop.Directive_string(use_output),
);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment