简体   繁体   中英

How can I substitute printfn with a noop

I use pf as an abbreviation of printfn

let pf = printfn

// signature:
val pf : (Printf.TextWriterFormat<'a> -> 'a)

Can we change pf so that it is a noop and does not output anything? I tried things like

let pf<'a> (x : (Printf.TextWriterFormat<'a> -> 'a)) = ()

// signature:
val pf : (Printf.TextWriterFormat<'a> -> 'a)

How about:

> let noprintf f = Printf.ksprintf (fun _ -> ()) f;;
val noprintf : f:Printf.StringFormat<'a,unit> -> 'a

> noprintf "Hello, %s!" "world";;
val it : unit = ()
>

Here is a simple solution

let pf fstring = 
    if false then 
        printfn fstring 
    else 
        fun _ -> Operators.Unchecked.defaultof<'a>;;

Note: this does have some problems and wont work in all cases - see comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM