Reputation: 42343
In the following F# code; I would expect that the printfn
is being called three times; each with a string. However, the bottom line does not compile (The type 'string' is not compatible with the type 'Printf.TextWriterFormat<'a>'
).
What is it about the first two lines that means this can work? Aren't they just strings too?
open System
printfn ("\r\n") // Works
printfn ("DANNY") // Works
printfn (DateTime.Now.ToLongTimeString()) // Doesn't compile
Upvotes: 18
Views: 5034
Reputation: 7735
@Lee's answer is correct as per a possible workaround, but it does not describe what happens with your code.
In an expression printf "foo"
, the "foo"
is not a string to be formatted. Instead, it is input formatter by itself. More specifically, it is a string literal used to infer the actual type of the TextWriterFormat<'T>
.
The signature of printf
is:
printf : TextWriterFormat<'T> -> 'T
Since printfn ("DANNY")
does not contain any format specifiers, the F# compiler infers a TextWriterFormat<unit>
, and the entire expression becomes printfn ("DANNY") ()
.
With a variable, it's impossible to statically predict what format specifiers will be there. Consider if ToLongTimeString()
method was able to return strings of "%s"
or "%d %d %d"
, what would be the prototype of the returned function?
When inferring the correct type, a string literal works fine, but the variable or let
-binding does not work:
let foo1 = "foo"
let bar = printf foo // does not compile
[<Literal>] let foo2 = "foo";; // see update below
let bar = printf foo2 // compiles fine
In any case, it looks much safer to always use format specifiers:
printf "%s" "DANNY"
printf "%s" (DateTime.Now.ToLongTimeString())
Update: don't forget to type double colon ;;
after [<Literal>]
value to avoid warning FS0058 in VS2013.
Upvotes: 10
Reputation: 144136
The F# compiler statically analyses the format strings you pass to printfn
to check that the arguments you pass are valid for the format specifiers you use. For example, the following does not compile:
printfn "%d" "some value"
since string
is not compatible with the %d format specifier. The compiler converts valid format strings into a TextWriterFormat<T>
.
It can't do this with arbitrary strings, and since it does not do the conversion, you get the type error above.
You can do the conversion yourself however using Printf.TextWriterFormat
.
For example, for a format string requiring a string
and an int
you can use:
let f = Printf.TextWriterFormat<string -> int -> unit>("The length of '%s' is: %d")
printfn f "something" 9
Since your string has no format placeholders, you can do:
let f = Printf.TextWriterFormat<unit>(DateTime.Now.ToLongTimeString())
printfn f
Upvotes: 22