Created
April 25, 2025 08:48
-
-
Save lxdlam/efe8f7aed56c1bb40f0a96a971da7817 to your computer and use it in GitHub Desktop.
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
| (** ---------- ACL phantom tags (no runtime values) ---------- *) | |
| type readonly (* no constructors – a compile-time marker *) | |
| type readwrite (* idem *) | |
| (** ---------- Generic driver ---------- *) | |
| type _ driver = { mutable value : int } (* 'acl is phantom *) | |
| (** Universally available *) | |
| let read (d : _ driver) = d.value | |
| (** Only for read-write drivers *) | |
| let write (d : readwrite driver) v = d.value <- v | |
| (** Constructors (factories) *) | |
| let new_readonly () : readonly driver = { value = 15 } | |
| let new_readwrite () : readwrite driver = { value = 15 } | |
| let read_more (d : _ driver) = d.value + 10 | |
| (* ---------------- Demo ---------------- *) | |
| let () = | |
| let drw = new_readwrite () in | |
| Printf.printf "%d\n" (read drw); (* 15 *) | |
| write drw 10; | |
| Printf.printf "%d\n" (read drw); (* 10 *) | |
| let dro = new_readonly () in | |
| Printf.printf "%d\n" (read dro); (* 15 *) | |
| (* write dro 10 *) (* - : compile-time error *) | |
| Printf.printf "%d\n" (read_more dro); (* 25 *) | |
| Printf.printf "%d\n" (read_more drw); (* 20 *) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment