I tried to write out a message to the command line using ‘printfn’. Here is my code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let message = "Hello world!" | |
printfn message // throws a compiler error |
Unfortunately this code doesn’t compile. Here is the compiler error I got:
The type 'string' is not compatible with the type 'Printf.TextWriterFormat<'a>'
How hard can it be to write a simple string message?
The problem becomes obvious when we look at the signature of the printfn function:
printfn : TextWriterFormat<'T> –> 'T
The function didn’t expect a string but a TextWriterFormat<'T>. The compiler has no clue how to convert our string variable to this type.
To fix this you should specify a format specification(%s in this case) and your variable:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let message = "Hello world!" | |
printfn "%s" message |