I have the following code that doesn’t work and I am not able to find why :
let open_socket addr port =
Lwt.catch
(fun () ->
let sock = Lwt_unix.socket Lwt_unix.PF_INET Lwt_unix.SOCK_STREAM 0 in
let sockaddr = Lwt_unix.ADDR_INET (addr, port) in
Lwt_unix.connect sock sockaddr
>>= fun () ->
Lwt.return sock
)
(function
| Unix.Unix_error (error, fn_name, param_name) -> Lwt.return ()
Lwt_io.eprintf "%s %s %s : unable to open socket. Exiting..." (Unix.error_message error) fn_name param_name
>>= fun () -> Lwt.return_nil
| e -> Lwt.fail e
)
The message error with Merlin is the following
This expression has type 'a list Lwt.t but an expression was expected of type Lwt_unix.file_descr Lwt.t Type 'a list is not compatible with type Lwt_unix.file_descr
I have used Lwt.catch
in a previous function without problem :
let gethostbyname name =
Lwt.catch
(fun () ->
Lwt_unix.gethostbyname name
>>= fun entry ->
let addrs = Array.to_list entry.Unix.h_addr_list in
Lwt.return addrs
)
(function
| Not_found -> lwt_print_err (name ^ ": Host not found")
>>= fun () -> Lwt.return_nil
| e -> Lwt.fail e
)