capitalex hat die Gist bearbeitet 2 hours ago. Zu Änderung gehen
1 file changed, 42 insertions
properties.ml(Datei erstellt)
| @@ -0,0 +1,42 @@ | |||
| 1 | + | module Univ = struct | |
| 2 | + | type t = .. | |
| 3 | + | let id = | |
| 4 | + | let id = ref 0 in | |
| 5 | + | fun () -> | |
| 6 | + | incr id ; | |
| 7 | + | !id | |
| 8 | + | ||
| 9 | + | module Embed (M : sig type t end) = struct | |
| 10 | + | type t += E of M.t | |
| 11 | + | let wrap m = E m | |
| 12 | + | let read = function E m -> Some m | _ -> None | |
| 13 | + | ||
| 14 | + | let new_property () = | |
| 15 | + | let id = id () in | |
| 16 | + | let set t v = | |
| 17 | + | Hashtbl.replace t id @@ wrap v in | |
| 18 | + | let get t = | |
| 19 | + | Hashtbl.find_opt t id |> Fun.flip Option.bind read in | |
| 20 | + | (set, get) | |
| 21 | + | end | |
| 22 | + | end | |
| 23 | + | ||
| 24 | + | module IntWrapper = Univ.Embed(Int) | |
| 25 | + | module StringWrapper = Univ.Embed(String) | |
| 26 | + | ||
| 27 | + | let and_null = Hashtbl.create 16 | |
| 28 | + | let or_watt = Hashtbl.create 16 | |
| 29 | + | ||
| 30 | + | let name = StringWrapper.new_property () | |
| 31 | + | let age = IntWrapper.new_property () | |
| 32 | + | let money = IntWrapper.new_property () | |
| 33 | + | ||
| 34 | + | let get t (set, get) = get t | |
| 35 | + | let set t (set, get) x = set t x | |
| 36 | + | ||
| 37 | + | let () = set and_null name "and Null" | |
| 38 | + | let () = set and_null age 28 | |
| 39 | + | let () = set and_null money 20 | |
| 40 | + | let () = set or_watt name "or Watt" | |
| 41 | + | let () = set or_watt age 28 | |
| 42 | + | let () = set or_watt money 100 | |
Neuer
Älter