Skip to content

Instantly share code, notes, and snippets.

@stedolan
Created March 15, 2021 14:02
Show Gist options
  • Save stedolan/286498f2837f3d9baa79db9b605d7598 to your computer and use it in GitHub Desktop.
Save stedolan/286498f2837f3d9baa79db9b605d7598 to your computer and use it in GitHub Desktop.
An Obj.magic tutorial
(* An Obj.magic tutorial. *)
(* Start with a function that does some integer computation *)
let f _ _ _ _ _ =
42_42_42_42+42_42_4-2_42
(* We're going to use OCaml's "magic increment" *)
let i = Obj.magic incr
let _ =
let f = ref f and g = ref f in
(* Increment f 9 times until we get the desired
function: in this case, print_string *)
i f; i f; i f;
i f; i f; i f;
i f; i f; i!f;
(* Now we can print things! *)
! g () () () "Hello, magic world!\n" 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment