How to define printfn equivalent in F#

前端 未结 2 647
南笙
南笙 2021-01-05 10:26

Since I do research with F# (in particular, using F# interactive), I\'d like to have switchable \"print-when-in-debug\" function.

I can do

let dprint         


        
2条回答
  •  长情又很酷
    2021-01-05 11:09

    You can use the kprintf function, which formats a string using the standard syntax, but then calls a (lambda) function you specify to print the formatted string.

    For example, the following prints the string if debug is set and otherwise does nothing:

    let myprintf fmt = Printf.kprintf (fun str -> 
      // Output the formatted string if 'debug', otherwise do nothing
      if debug then printfn "%s" str) fmt
    

提交回复
热议问题