I'm creating a chat server, and I have a function that handles login. There exists a preset ref called nick and a preset input stream imp. My code is as follows:
let handle_login nr (inp,outp) =
Lwt_io.printl "<Enter your 'nick'name>" >>= Lwt.return(nick := (Lwt_io.read_line inp))
However, this code give me the error:
Error: This expression has type string Lwt.t
but an expression was expected of type string.
I know that the following code does work:
let handle_login nr (inp,outp) =
Lwt_io.printl "<Enter your 'nick'name>" >>= Lwt.return(nick := "Jane")
In short, I don't know how to assign vars to values obtained from threads.
I'm not very familiar with Lwt
, but if it works like any other monad I would think this should work:
let handle_login nr (inp, outp) =
Lwt_io.printl "<Enter your 'nick'name>"
>>= fun () -> Lwt_io.read_line inp
>>= fun str -> Lwt.return (nick := str)
But I have to point out as well that mutating shared state from async code is a disaster waiting to happen. That you can do it certainly doesn't mean you should.