let share state =
let rec f1 ... = ... you can use state freely here ...
and f2 ... = ... same here ...
and f3 ... = ... same here ...
and f4 ... = ... same here ...
in
f1, f2, f3, f4
let state = ...
let f1, f2, f3, f4 = share state
如果你想「狀態」是這將是例如一個模塊:
module type S = sig ... end
let share m =
let module M = (val m : S) in
let rec f1 ... = ... use M at will ...
and f2 ... = ... use M at will ...
and f3 ... = ... use M at will ...
and f4 ... = ... use M at will ...
in
f1, f2, f3, f4
module M : S = struct ... end
let f1, f2, f3, f4 = share (module M)
如果你希望得到的fi
一個模塊中進行包裝,然後用一個仿函數
module type S = sig
val f1 : ...
val f2 : ...
val f3 : ...
val f4 : ...
end
module type State : sig ... end
module Make (M : State) : S = struct
let rec f1 ... = ... use M at will here ...
and f2 ... = ... again ...
and f3 ... = ... again ...
and f4 ... = ... again ...
end
module State : State = struct ... implement state... end
module Instance = Make (State)
let() = Instance.f1()...
清潔,整潔,優雅!太好了! – user34812