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
utopis 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.tUpdate:
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'ais thereforeunit. The second parameter of(>>=)isf.ftakes aunit, which is what we needed, as'aisunit. It returns aunit Lwt.t, so'bis alsounit. This means that the final result will be aunit Lwt.t.