Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:46:46+00:00 2026-06-07T22:46:46+00:00

I want to add debug printing to my project with a function having a

  • 0

I want to add debug printing to my project with a function having a type signature something like:

bool -> Printf.TextWriterFormat<'a> -> 'a

i.e. it should take a bool indicating whether or not we are in verbose mode, and use that to take the decision about whether to print or not.

For example, lets say dprint : bool -> Printf.TextWriterFormat<'a> -> 'a then I would like this behaviour:

> dprint true "Hello I'm %d" 52;;
Hello I'm 52
val it : unit = ()
> dprint false "Hello I'm %d" 52;;
val it : unit = ()

The idea is that a command line flag can be used to avoid control this output. I also want to avoid a runtime cost in the “not verbose” case. It is possible to define a function that works like this using kprintf:

let dprint (v: bool) (fmt: Printf.StringFormat<'a,unit>) =  
  let printVerbose (s: string) =
    if v then System.Console.WriteLine(s)

  fmt |> Printf.kprintf printVerbose

but printing/ignoring a sequence of numbers with List.iter (dprint b "%A") [1..10000] (b \in {true,false}) takes amount 1.5s for both values of b on my machine.

I came up with another method using reflection that builds an appropriately typed function to discard the formatting arguments:

let dprint (v: bool) (fmt: Printf.TextWriterFormat<'a>) : 'a =
  let rec mkKn (ty: System.Type) =
    if FSharpType.IsFunction(ty) then
      let _, ran = FSharpType.GetFunctionElements(ty)
      FSharpValue.MakeFunction(ty,(fun _ -> mkKn ran))
    else
      box ()
  if v then
    printfn fmt
  else
    unbox<'a> (mkKn typeof<'a>)

but here the reflection seems too expensive (even more so than that done inside the standard libraries complicated definition of printf sometimes).

I don’t want to litter my code with things like:

if !Options.verbose then
    printfn "Debug important value: %A" bigObject5

or closures:

dprint (fun () -> printfn "Debug important value: %A" bigObject5)

so, are there any other solutions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:46:48+00:00Added an answer on June 7, 2026 at 10:46 pm

    I like your solution using reflection. How about caching it on the type level so that you pay the price of reflection only once per type? For example:

    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 ()
    
    [<Sealed>]
    type Format<'T> private () =
        static let instance : 'T =
            unbox (mkKn typeof<'T>)
        static member Instance = instance
    
    let inline dprint verbose args =
        if verbose then
            printfn args
        else
            Format<_>.Instance
    

    A pragmatist would just use the fast C# formatted printing machinery instead of this. I avoid Printf functions in production code because of the overhead they have, as you point out. But then F# printing definitely feels nicer to use.

    My #time results for List.iter (dprint false "%A") [1..10000]:

    • Original version : 0.85
    • Original version with reflection : 0.27
    • The proposed version : 0.03
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add new contact, when i debug i didnt find any error
I want to add debug metadata to my generated llvm IR, which is created
I want to add some feature to this project and I need smartgwt for
I want to add some log.debug statements to a class I'm working on, and
I have 1 function in my debug model which i want to use in
I want add UIGestureRecognizerDelegate to UIWebView,but failed. if [self.view addsubView:webView]; So UIWebView is ok,but
I want add new Feed item on entity persist and update. I write this
I want add label to every row in tableview at right side of the
I want add my custom sidebar next right column all page. Please check this
I have a website built with ASP.NET (3.5) and want add some level of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.