-
-
Save joelreymont/887334 to your computer and use it in GitHub Desktop.
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
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 Message = | |
sig | |
type t | |
val make : unit -> t | |
val print : t -> unit | |
end | |
module Server (Req : Message) (Rep : Message) = | |
struct | |
let server convert dispatch = | |
while true do | |
let req = Req.make () in | |
let var = convert req in | |
let rep = dispatch var in | |
Req.print req; | |
Rep.print rep | |
done | |
end | |
module MyRequest = | |
struct | |
type t = int | |
let make () = (1 : t) | |
let print o = () | |
end | |
module MyReply = | |
struct | |
type t = int | |
let make () = (2 : t) | |
let print o = () | |
end | |
(* | |
Given that I will never use MyServer with convert and dispatch | |
other than the ones below, is there a way to functorize this? | |
I would love to join types req and rep, as well as convert | |
and dispatch into a module that I could specializer Server on. | |
*) | |
type req = | A of MyRequest.t | |
type rep = | B of MyReply.t | |
let convert req = A req | |
let dispatch = function | |
| A _ -> MyReply.make () | |
module MyServer = Server (MyRequest) (MyReply) | |
let _ = MyServer.server convert dispatch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment