public
Created

  • Download Gist
DPrint.fs
F#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 
let rec mkKn (ty: System.Type) =
if Reflection.FSharpType.IsFunction(ty) then
let _, ran = Reflection.FSharpType.GetFunctionElements(ty)
// NOTICE: do not delay `mkKn` invocation until runtime
let f = mkKn ran
Reflection.FSharpValue.MakeFunction(ty, fun _ -> f)
else
box ()
 
let dprint (v: bool) (fmt: Printf.TextWriterFormat<'a>) : 'a =
if v then
printfn fmt
else
unbox<'a> (mkKn typeof<'a>)
 
[<Sealed>]
type Format<'T> private () =
static let instance : 'T =
unbox (mkKn typeof<'T>)
static member Instance = instance
 
let dprint2 verbose args =
if verbose then
printfn args
else
Format<_>.Instance
 
#time
 
for i in 0 .. 10000 do
dprint false "%i" i // 1 sec
 
for i in 0 .. 10000 do
dprint2 false "%i" i // 0.001 sec

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.