Skip to content

Instantly share code, notes, and snippets.

@afeinberg
Created April 18, 2016 09:46
Show Gist options
  • Save afeinberg/8ed5728cbd2b15e0d0b9005bc7c3a6b5 to your computer and use it in GitHub Desktop.
Save afeinberg/8ed5728cbd2b15e0d0b9005bc7c3a6b5 to your computer and use it in GitHub Desktop.
module type EXAMPLE = sig
class example_class : int ->
object
method get_i : int
method compare : example_class -> bool
end
end
module Example : EXAMPLE = struct
class example_class (i : int) =
object
val mutable i' = i
method get_i = i'
method compare (other : example_class) = other#get_i == i'
end
end
module Experiment : sig
type t
class experiment_class' : int ->
object
method get_i : int
method compare : t -> bool
end
val cast : t -> experiment_class'
val make : int -> t
end = struct
class experiment_class (i:int) =
object
val mutable i' = i
method get_i = i'
method compare (other: experiment_class) = other#get_i == i'
end
type t = experiment_class
class experiment_class' = experiment_class
let cast x = x
let make i = new experiment_class' i
end
(* Commented out lines won't compile! *)
let demo () =
let x = object
method get_i = 3
method compare _ = false
end in
let y = new Example.example_class 3 in
let z = Experiment.make 2 in
let z'= Experiment.make 2 in
let z'' = Experiment.make 3 in
[ x#compare y ;
(Experiment.cast z)#compare z ;
y#compare x ;
(* (Experiment.cast z)#compare y ; *)
(* y#compare (Experiment.cast z) ; *)
(Experiment.cast z)#compare z'' ;
(Experiment.cast z)#compare z' ;
(Experiment.cast z')#compare z ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment