I'm confused why the Lwt print function Lwt_io.print
has type
string -> unit Lwt.t
But if I run Lwt_io.print "a" >>= fun () -> Lwt_io.print "b";;
the result is that "ab" is printed and type unit is returned.
I would imagine that this would be a type error since Lwt_io.print returns unit Lwt.t not unit. Why is the second part of the thread called?
I suspect you are getting confused because
utop
is being smart.If you look at the utop documentation, it is written
Which is why
appears to be of type
unit
. To see the real type, try the followingYou will see that as you get what you were expecting, a
unit Lwt.t
Update:
Just to make things clear about types, we have
Lwt_io.print "a"
therefore returns aunit Lwt.t
. This is the first parameter of(>>=)
, and'a
is thereforeunit
. The second parameter of(>>=)
isf
.f
takes aunit
, which is what we needed, as'a
isunit
. It returns aunit Lwt.t
, so'b
is alsounit
. This means that the final result will be aunit Lwt.t
.