开发者

How can I trigger an action after an OCaml printf with user-supplied format string

I have a simple function that prints a string and exits:

let fatal s = 
  print_string "Log: ";
  print_endline s;
  exit 1

I can use printf to do something similar without the exit 1:

let log fmt = 
  printf ("Log: " ^^ fmt)

This log function takes a format string and returns a function that takes the parameters needed for that format string and prints "Log: " in front. (Of course my prefix isn't this simple for my real application.)

Taking these two and combining them is not easy. A first attempt:

let fatalf fmt =
   Printf.printf ("Log: " ^^ fmt) 
   ???
   exit 1

The problem is that I have to return the result of my printf expression so that the remaining arguments can be applied to it. Once I've returned this value, I don't have flow control anymore to run exit.

The printf formatter %t looks useful, as it takes a function and runs it:

printf ("Log: " ^^ fmt ^^ "%!%t") ... (fun _ -> exit 1)

This doesn't seem to work as the %t must be last so it's run after the log message is written, but this means that the exit function must be after user-specified parameters, and since there's no way to know how many parameters there will be intervening, one can't generate a closure that does the full application of printf when given the intervening arguments.

I recall there being some support for named printf arguments, but that this was pulled as it was buggy. Is there any way to emulate that, or to achieve the desired "exit开发者_C百科 after arbitrary printf" behavior?


You're looking for Printf.kprintf:

let fatalf fmt =
  Printf.kprintf (fun str ->
    Printf.eprintf "Fatal error: %s !\n%!" str;
    exit 1) fmt

kprintf takes a continuation with type string -> 'a and applies it to the result of sprintf-ing with the supplied format. The result of the continuation is the result of the entire call, as expected.


printf is a tricky part of OCaml because it's supported by compiler magic. If the source contains a string constant and its type environment requires a printf format, the compiler magically converts the string constant to a format. This is pretty slick, but once you pass beyond the simplest uses you sometimes need to do the magic yourself. In particular, note that it has to be a string constant in order for the compiler to be able to enforce strong typing.

Your second example doesn't show an s parameter, but I figure you just left it off. If you just want to print one parameter using a format that's also passed as a parameter, you can do something like this:

let fatalfs f s =
    printf ("Log:" ^^ f) s;
    exit 1

The ^^ operator concatenates two formats into one format.

Here's a session with this function:

$ rlwrap ocaml312
    Objective Caml version 3.12.0

# let fatalfs f s = Printf.printf ("Log: " ^^ f) s; exit 1;;
val fatalfs :
  ('a -> 'b, out_channel, unit, unit, unit, unit) format6 -> 'a -> 'c = <fun>
# fatalfs "Here is the value: [%s]\n" "value";;
Log: Here is the value: [value]
$ echo $?
1
$

Note that fatalfs is actually polymorphic in the type of the parameter s. It works as long as the type of the format string and the second parameter match up. It's pretty impressive.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜