Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>printfn</code> and its various cousins have several advantages:</p> <ul> <li>They're <strong>shorter</strong>.</li> <li>They <em>can</em> do some <strong>static type checking</strong>; i.e. <code>printfn "%d" "bad type"</code> will not compile.</li> <li>...but you don't have to do static type checking; <code>%O</code> prints any object</li> <li>They can print <strong>"smart" representations</strong> for things like arrays, tuples, and discriminated unions with <code>%A</code></li> <li>They can be partially applied; i.e. <code>printfn "%d, %d" 3</code> is a valid expression. This is particularly nifty since the compiler can check that you actually apply the right number of arguments when you later use this subexpression - unlike <code>Console.WriteLine</code> which will happily accept too many or too few parameters.</li> </ul> <p>In practice, the most common partial application is likely to include just the format string; e.g.</p> <pre><code>let printParticle = printfn "Particle at (%d, %d), state %A, p = %f" printParticle 2 3 //compile time warning about ignored value printParticle 3 4 someState 0.4 //fine printParticle 5 6 someState 0.4 0.7 //compile-time error </code></pre> <p>However, prior to F# 3.1, it's also <strong>slow</strong>. It's plenty fast enough to keep up with you the coder, but if you're using it in some form of serialization, it could turn into a bottleneck. The <a href="http://blogs.msdn.com/b/fsharpteam/archive/2013/06/27/announcing-a-pre-release-of-f-3-1-and-the-visual-f-tools-in-visual-studio-2013.aspx" rel="noreferrer">F# 3.1 release announcement</a> (which is distributed as part of Visual Studio 2013) claims to improve the performance dramatically, though I have not verified this.</p> <p>Personally, I usually use printfn for exploratory coding, and then I largely stick to <code>%A</code> with the occasional other specifier thrown in. However, the .NET native string formatting is still useful in some cases for its detailed culture and formatting-related options. If you want maximum speed direct concatenation (or a <code>StringBuilder</code>) will easily outperform both as this avoids interpreting the format string.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload