Thomas
Thomas

Reputation: 12107

where is sprintfn gone, in F#?

There are a few pages that mention sprintfn in F#.

One of them is a classic: https://fsharpforfunandprofit.com/posts/fsharp-in-60-seconds/

But also here, in an answer by Tomas: Is the "expression problem" solvable in F#?

However, I can't seem to be able to use it, it looks like it is not defined. Was it removed from the language?

Upvotes: 4

Views: 143

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243041

As far as I can see, this function is not available anywhere in the stndard F# library today. Given that we have printf/printfn, this sounds like a possibly useful addition. You can certainly define it yourself:

let sprintfn fmt = 
  Printf.kprintf (fun s -> s + "\n") fmt

sprintfn "Hello %s!" "world"

To answer the question where is it gone - I'm not entirely sure this ever existed. My SO answer is only a sketch and not an executable code, so that may just have been an error. The code shared from Scott uses the function in a comment only.

There was certainly an old Visual Studio "F# Script File" template (I can find a copy on my machine from circa 2010), which had the following:

/// A string computed using the 'sprintfn' string layout function
let stringE = sprintf "stringC = %s, stringD.Length = %d" stringC stringD.Length

If you do a search on all of GitHub, you'll find a few references to this, but most of those are just copies of the code from Scott. There are a few libraries that define their own sprintfn function, but not very many.

Amazingly, you can also download the F# code drop from November 2010 from WebArchive and this also has no mention of sprintfn. So, I really think it never existed :-).

Upvotes: 6

Related Questions