The following code does not compile:
let x = "hello" in
Printf.printf x
The error is:
Error: This expression has type string but an expression was expected of type
('a, out_channel, unit) format =
('a, out_channel, unit, unit, unit, unit) format6
1) Can someone give an explanation of the error message?
2) And why would a string cannot be passed to printf ?
The first argument to printf must be of type
('a, out_channel, unit) formatnot string. String literals can be automatically converted to an appropriate format type, but strings in general can’t.The reason for that is that the exact type of a format string depends on the contents of the string. For example the type of the expression
printf "%d-%d"should beint -> int -> ()while the type ofprintf "%s"should bestring -> (). Clearly such type checking is impossible when the format string is not known at compile time.In your case you can just do
printf "%s" x.