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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…