Created
March 15, 2021 14:02
-
-
Save stedolan/286498f2837f3d9baa79db9b605d7598 to your computer and use it in GitHub Desktop.
An Obj.magic tutorial
This file contains hidden or 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
(* 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