Created
June 27, 2021 10:35
-
-
Save idkjs/2c21d26248d4e398928642e6bac5b9d4 to your computer and use it in GitHub Desktop.
try a function example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let try_finally = (~always, f) => | |
switch (f()) { | |
| x => | |
always(); | |
x; | |
| exception e => | |
always(); | |
raise(e); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
https://dune.readthedocs.io/en/stable/toplevel-integration.html#toplevel-integration