Converting OCaml strings to format6

倖福魔咒の 提交于 2020-01-02 02:03:32

问题


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 ?


回答1:


The first argument to printf must be of type ('a, out_channel, unit) format not 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 be int -> int -> () while the type of printf "%s" should be string -> (). 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.




回答2:


As sepp2k points out, in OCaml printf formats have a distinct type, and are not simply strings. String literals are converted automatically to printf formats, but x is not a string literal. If you want to give a name to a format, you can convert it explicitly yourself:

> let x = format_of_string "hello" in Printf.printf x
hello- : unit = ()

You can also cause the implicit conversion by specifying a type for x, but the types of formats are so complicated this is quite painful:

# let (x: ('a,'b,'c,'d,'d,'a) format6) = "hello" in Printf.printf x;;
hello- : unit = ()

(I personally don't understand the format6 type.)



来源:https://stackoverflow.com/questions/10458672/converting-ocaml-strings-to-format6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!