Created
September 1, 2011 22:47
-
-
Save avsm/1187501 to your computer and use it in GitHub Desktop.
hotplug with objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module type DEVICE = sig | |
type t | |
val create: string -> t | |
val id: t -> string | |
val read: t -> string | |
end | |
module Dummy : DEVICE = struct | |
type t = string | |
let create id = id | |
let id t = "dummy"^t | |
let read t = "dumdum" | |
end | |
module Real : DEVICE = struct | |
type t = string * int (* a different internal type from Dummy *) | |
let create id = (id,0) | |
let id (t,_) = "real"^t | |
let read (t,_) = "realreal" | |
end | |
module TypEq : sig | |
type ('a, 'b) t | |
val apply: ('a, 'b) t -> 'a -> 'b | |
val refl: ('a, 'a) t | |
val sym: ('a, 'b) t -> ('b, 'a) t | |
end = struct | |
type ('a, 'b) t = ('a -> 'b) * ('b -> 'a) | |
let refl = (fun x -> x), (fun x -> x) | |
let apply (f, _) x = f x | |
let sym (f, g) = (g, f) | |
end | |
(* runtime representations of the interfaces as Typ.typ *) | |
module Typ = struct | |
type 'a typ = | |
| Dummy of ('a, Dummy.t) TypEq.t | |
| Real of ('a, Real.t) TypEq.t | |
end | |
(* the types themselves *) | |
let dummy = Typ.Dummy TypEq.refl | |
let real = Typ.Real TypEq.refl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type device = < id: string; read: string; > | |
let dummy id : device = | |
object | |
method id = "dummy"^id | |
method read = "dummy" | |
end | |
let real id : device = | |
let fd = 0 in | |
object | |
method id = "real"^id | |
method read = "fromfd"^(string_of_int fd) | |
end | |
let providers = | |
let h = Hashtbl.create 1 in | |
Hashtbl.add h "d1" dummy; | |
Hashtbl.add h "d2" real; | |
h | |
let manager fn = | |
Hashtbl.iter (fun id dev_fn -> | |
let dev = dev_fn id in | |
fn id dev | |
) providers | |
let _ = | |
manager (fun id dev -> | |
Printf.printf "%s : %s\n%!" dev#id dev#read | |
) |
raphael-proust
commented
Sep 2, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment