Skip to content

Instantly share code, notes, and snippets.

@marctrem
Created May 1, 2019 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marctrem/702903d5e53ba939610353d0ba963ab1 to your computer and use it in GitHub Desktop.
Save marctrem/702903d5e53ba939610353d0ba963ab1 to your computer and use it in GitHub Desktop.
Phantom Bulider
#!/usr/bin/env ocaml
#use "topfind"
#thread
#require "core"
open Core
type name_unset
type name_set
type age_unset
type age_set
type t = {
name : string ;
age : int ;
}
module Builder : sig
type ('name, 'age) b
val empty : unit -> (name_unset, age_unset) b
val set_name : ('name, 'age) b -> string -> (name_set, 'age) b
val set_age : ('name, 'age) b -> int -> ('name, age_set) b
val build : (name_set, age_set) b -> t
end =
struct
type ('name, 'age) b = {
name : string option ;
age : int option ;
}
let empty () = {
name = None ;
age = None ;
}
let set_name b name = {
b with
name = Some name
}
let set_age b age = {
b with
age = Some age
}
let build b : t =
{
name = Option.value_exn b.name ;
age = Option.value_exn b.age ;
}
end
let () =
let b = Builder.empty in
let built = Builder.build b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment