File: override_example.ml

package info (click to toggle)
ppx-hash 0.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 316 kB
  • sloc: ml: 1,961; ansic: 180; makefile: 14; sh: 11
file content (63 lines) | stat: -rw-r--r-- 1,230 bytes parent folder | download | duplicates (2)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
open Ppx_hash_lib.Std
open Hash.Builtin

module Barbins_example = struct
  module X = struct
    type t = int [@@deriving hash]
  end

  module Y = struct
    type y = int [@@deriving hash]
  end

  module Z = struct
    type t = int [@@deriving hash]
  end

  module Example = struct
    module T = struct
      type t =
        { x : X.t
        ; y : Y.y
        ; z : Z.t
        ; mutable cached : int option [@hash.ignore]
        }
      [@@deriving hash]
    end

    module T_type = struct
      type t = T.t =
        { x : X.t
        ; y : Y.y
        ; z : Z.t
        ; mutable cached : int option
        }
    end

    module Unmemoized = T

    module Memoized = struct
      include T_type

      let hash t =
        match t.cached with
        | Some x -> x
        | None ->
          let cached = T.hash t in
          (* dont use state *)
          t.cached <- Some cached;
          cached
      ;;

      let hash_fold_t (state : Hash.state) (t : t) = hash_fold_int state (hash t)

      (* use state *)
    end
  end

  module Client = struct
    (* ensure overrides are typed correctly *)
    type t1 = Example.Unmemoized.t [@@deriving hash]
    type t3 = Example.Memoized.t [@@deriving hash]
  end
end