package bechamel

  1. Overview
  2. Docs

Source file ext.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(* (c) Frédéric Bour *)

module Make (Functor : S.FUNCTOR) = struct
  type t = ..

  module type Extension = sig
    type x
    type t += T of x
  end

  type 'a extension = (module Extension with type x = 'a)
  type instance = V : 'a * 'a Functor.t -> instance

  let handlers = Hashtbl.create 16

  module Injection (X : sig
    type t

    val instance : t Functor.t
  end) : Extension with type x = X.t = struct
    type x = X.t
    type t += T of x

    let () =
      let instance = X.instance in
      Hashtbl.add handlers
        (Stdlib.Obj.Extension_constructor.id [%extension_constructor T]
         [@warning "-3"])
        (function T x -> V (x, instance) | _ -> raise Not_found)
  end

  let inj (type a) (f : a Functor.t) : a extension =
    (module Injection (struct
      type t = a

      let instance = f
    end))

  let rec iter t lst =
    let[@warning "-8"] (f :: r) = lst in
    try f t with _ -> (iter [@tailcall]) t r

  let prj (t : t) =
    let uid =
      Stdlib.Obj.Extension_constructor.((id (of_val t) [@warning "-3"]))
    in
    iter t (Hashtbl.find_all handlers uid)
end
OCaml

Innovation. Community. Security.