Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created November 11, 2019 21:23
Show Gist options
  • Save fakenickels/8dc927ab061b92272920f7958b59bd37 to your computer and use it in GitHub Desktop.
Save fakenickels/8dc927ab061b92272920f7958b59bd37 to your computer and use it in GitHub Desktop.
module type SERIALIZABLE = sig
type t
val to_string: t -> string
val of_string: string -> t
end;;
implicit module SInt : SERIALIZABLE with type t = int = struct
type t = int
let to_string = string_of_int
let of_string = int_of_string
end
implicit module SFloat : SERIALIZABLE with type t = float = struct
type t = float
let to_string = string_of_float
let of_string = float_of_string
end
implicit module SBoolean : SERIALIZABLE with type t = bool = struct
type t = bool
let to_string = string_of_bool
let of_string = bool_of_string
end
let to_string (type a)
{S: SERIALIZABLE with type t = a} = S.to_string;;
let of_string (type a)
{S: SERIALIZABLE with type t = a} = S.of_string;;
let myNumber = to_string 3;;
let myNumberFloat = to_string 3.0;;
let myBool = to_string true;;
let () = print_endline myNumber;;
let () = print_endline myNumberFloat;;
let () = print_endline myBool;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment