Angle.Bracket
Angle.Bracket

Reputation: 1520

What exactly is the return type of the printfn function in F#?

I looked up the documentation of printfn here and this is what it says:

printfn format

Print to stdout using the given format, and add a newline.

format : TextWriterFormat<'T> The formatter.

Returns: 'T The formatted result.

But if I type the following in FSI

> let v = printfn "Hello";;
Hello
val v : unit = ()

it states that v (the return value of printfn) is of type unit.

This seems inconsistent, but I guess I am missing something here, so can anybody help me out ?

Upvotes: 3

Views: 265

Answers (1)

Brian Berns
Brian Berns

Reputation: 17038

It's not inconsistent. In your example, the format is of type TextWriterFormat<unit>, so the final return type is unit, because 'T is unit.

If you had written let v = printfn "Hello %s", then the type of v would be string -> unit. This is how F# provides type-safe string formatting.

Upvotes: 6

Related Questions