How to enter a format string literal?

Format string literals can be created and inferred correctly like:

let hello = "" ^^ "Hello, %s!"

let () = Printf.printf hello "Bob"

Is there a more direct way of entering the hello format string literal than using ^^?

You need to explicitely ask the typer for the format type. The Stdlib module provides a function format_of_string that does that for you, but it’s even longer than ^^.

Another way is to write let hello : _ format6 = "hello %s".

Best wishes,
Nicolás

Thanks folks. I assume format_of_string was intended to be the ‘canonical’ way to do it.

format_of_string is essentially given by let format_of_string (x : _ format6) = x, so I would say both ways are equally canonical.

Best wishes,
Nicolás

1 Like