Feb. 25, 2024, 2:34 a.m. | Yawar Amin

DEV Community dev.to

OCAML doesn't have string interpolation, but it does have C-style format strings (but type-safe). Here's an example:



let hello name = Printf.printf "Hello, %s!\n" name
(* Can be written as: let hello = Printf.printf "Hello, %s!" *)


This is type-safe in an almost magical way (example REPL session):



# hello 1;;
Error: This expression has type int but an expression was expected of type
string


It can however be a little tricky to wrap your head around:



# let bob …

error example format hello ocaml session string strings style type

Lead Developer (AI)

@ Cere Network | San Francisco, US

Research Engineer

@ Allora Labs | Remote

Ecosystem Manager

@ Allora Labs | Remote

Founding AI Engineer, Agents

@ Occam AI | New York

AI Engineer Intern, Agents

@ Occam AI | US

AI Research Scientist

@ Vara | Berlin, Germany and Remote