Skip to content

Instantly share code, notes, and snippets.

@yrashk

yrashk/traits.ml Secret

Created May 23, 2022 07:17
Show Gist options
  • Save yrashk/6233141df17bfe2811460a92a0f8bd19 to your computer and use it in GitHub Desktop.
Save yrashk/6233141df17bfe2811460a92a0f8bd19 to your computer and use it in GitHub Desktop.
open Traits
module type I = sig
include PartialEq.T
include PartialOrd.T
end
(* Admittedly, a bit wordy *)
let test (type a)
(module M : I
with type PartialEq.t = a
and type PartialEq.rhs = a
and type PartialOrd.t = a
and type PartialOrd.rhs = a) x y =
PartialOrd.partial_cmp (module M) x y
(* Can be used to provide multiple traits through a single module,
* we don't need to know which function is being used (eq? partial_cmp?) *)
let _ = test (module Int) 1 2;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment