How does one printf unit?

I tried:

let y  = (3 > 1; print_string "\n\nHi\n");;
Printf.printf "%a\n" s;;

but its of type unit and I can’t print it. Why?

In the command line:

# (3 > 1; print_string "\n\nHi\n")
  ;;
Warning 10: this expression should have type unit.


Hi
- : unit = ()

Why do you need to print the unit value? And what is s in your example? And what are you trying to do?

Sorry for answering with questions, but it looks like an xy-problem, so I want to catch you before you got totally lost :slight_smile:

Oh I just saw the statement:

(3 > 1; print_string "\n\nHi\n")

and was curious to see what its output was…but I was only able to see it on the command line. So I was curious whats going on. Perhaps my error/weird question stems from me also not knowing what unit is.

Ah. Perhaps what you’re looking for, is the type of 3 > 1 ?? It would be bool. The compiler is telling you that in a semicolon-sequence (viz. A ; B) the expression A does not have type unit, hence perhaps you (the programmer) didn’t mean to be computing and then discarding the result of A.

Does this help?

Unit is a type with a single value: ().
One could write a printer for unit as:

let string_of_unit () = "()" 

But then since a value of term unit is necessarily a constant (), it is possible to simply remove the argument

let string_of_unit = "()" 

And it is a general rule with unit: a value of type unit does not contain any information. So when a function returns unit, it is telling you that it does not return any meaningful information. In particular, it is a sign that the function is effectful: a pure function that only returns unit could be directly replaced by unit.

For instance, the type of print_string: string -> unit is literally telling us that print_string takes a string as an argument, and then does not return any information to the caller. The implication here is that print_string is doing an effect with its string argument, specifically printing it.