Skip to content

Instantly share code, notes, and snippets.

@t0yv0
Created July 19, 2012 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t0yv0/3145242 to your computer and use it in GitHub Desktop.
Save t0yv0/3145242 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment