Source file WriteOnceRef.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
(******************************************************************************)
(*                                                                            *)
(*                                  Inferno                                   *)
(*                                                                            *)
(*                       François Pottier, Inria Paris                        *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the MIT License, as described in the file LICENSE.               *)
(*                                                                            *)
(******************************************************************************)

type 'a t =
  'a option ref

let create () =
  ref None

let set r x =
  match !r with
  | None ->
      r := Some x
  | Some _ ->
      (* error: double write! *)
      assert false

let get r =
  match !r with
  | None ->
      (* error: read before write! *)
      assert false
  | Some x ->
      x