-
Notifications
You must be signed in to change notification settings - Fork 21
Open
Labels
Description
It is currently a bit difficult to define functions that are generic on the log function:
# let f log x y =
log (fun m -> m "start");
let v = x + y in
log (fun m -> m "result: %d" v);
v;;
Error: This expression has type 'a but an expression was expected of type int -> 'a
The type variable 'a occurs inside int -> 'a
It seems the simplest would be to define:
type 'a Logs.func = { log : 'a. 'a Logs.log }Functions that want to be generic on log function then need to take a 'a Logs.func argument:
# type 'a func = { log : 'a. 'a Logs.log };;
type 'a func = { log : 'a0. 'a0 Logs.log; }
# let f log x y =
log.log (fun m -> m "start");
let v = x + y in
log.log (fun m -> m "result: %d" v);
v;;
val f : 'a func -> int -> int -> int = <fun>
# f { log = Logs.warn } 4 2;;
ocaml: [WARNING] start
ocaml: [WARNING] result: 6
- : int = 6
# f { log = Logs.err } 4 2;;
ocaml: [ERROR] start
ocaml: [ERROR] result: 6
- : int = 6
Is there maybe a simpler solution that I fail to see ? (/cc @yallop @Drup)