From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p8FCMaQP014255 for ; Thu, 15 Sep 2011 14:22:36 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap0BAJvtcU7RVdK2mGdsb2JhbABDhFWicAgUAQEBAQEICQ0HFCaBUwEBAQECARICDwQZARseAwELBgMCCwMMAiYCAiIBEQEFARwGGxqHVZoOCop+QoJahRM7iG0CAwaBJoQ3gREEh2mLXox/PYNx X-IronPort-AV: E=Sophos;i="4.68,387,1312149600"; d="scan'208";a="119953593" Received: from mail-iy0-f182.google.com ([209.85.210.182]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 15 Sep 2011 14:22:30 +0200 Received: by iadk27 with SMTP id k27so2588844iad.27 for ; Thu, 15 Sep 2011 05:22:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=QYtXeHBHSTrwjDtemizB5M9pmUxiKTmpXfhi0qwDR1M=; b=UHcroVNMiWJJ/ftV+UwQHiNb+XxwGFXsplWxdzNIMov73SbJTb4znVtDBXirHnbJ3W w0YXq9c7y2PiuY9tYVWjkBAYHjUKTDuGs5eVu8u94J38hQAAPfN8QjjwhSHOgaXIl0JT bjarDq5uNJ5VP6BZ2gL3/Q8c+a4W6vxCttTCw= MIME-Version: 1.0 Received: by 10.42.96.72 with SMTP id i8mr1077475icn.60.1316089349584; Thu, 15 Sep 2011 05:22:29 -0700 (PDT) Received: by 10.43.50.6 with HTTP; Thu, 15 Sep 2011 05:22:29 -0700 (PDT) In-Reply-To: <4E71CDB8.5020704@dogguy.org> References: <20110913183714.GA15241@yeeloong.happyleptic.org> <4E71CDB8.5020704@dogguy.org> Date: Thu, 15 Sep 2011 15:22:29 +0300 Message-ID: From: Dmitry Grebeniuk To: Mehdi Dogguy Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p8FCMaQP014255 Subject: Re: [Caml-list] Lwt and exceptions Hello. > In fact, I was wondering > if Lwt's authors would be against adding a function like: > >        let wrap f x = try Lwt.return (f x) with e -> Lwt.fail e Not a very good idea, since evaluation of wrap'ped function's arguments can raise an exception that won't be caught by wrap, suppose: let lwt_string_of_int = wrap string_of_int let () = Lwt_main.run ( lwt_string_of_int (raise Exit) >>= fun s -> Lwt_io.printf "everything's okay buddies, it's %s\n" s ) So it's up to the caller to guarantee the absence of native exceptions. It's bad. If I will ever need to write such wrapper, I will make it to have type wrap : (unit -> ('a -> 'b)) -> (unit -> 'a) -> 'b Lwt.t maybe with the name of function too, and with early evaluation of first argument, like let wrap ?(funcname="") u_f = let r_f = try `Ok (u_f ()) with e_f -> `Error e_f in match r_f with | `Ok f -> fun x -> try Lwt.return (f x) with e_x -> (* 1.either *) Lwt.fail (Failure (Printf.sprintf "error evaluating function %S: %s" funcname (Printexc.to_string e_x))) (* 1.or *) Lwt.fail e_x | `Error e_f -> (* 2.either *) failwith (Printf.sprintf "the wrapped function %S itself \ raises error: %s" funcname (Printexc.to_string e_f)) (* 2.or *) fun _u_x -> Lwt.fail e_f (I haven't checked this code! I don't remember where should I set begin-end or parenthesis in nested matches in the original syntax, and I don't want to remember it now.) I like the uniform error handling. So I have wrapped the common IO-specific functions to a library that gives IO_lwt for lwt, IO_direct for direct OCaml I/O (stdlib), and... functor functor functor it sometimes. It uses "bind" and "return", but I'm very unsure whether the code can be called "monadic IO", since lwt itself does not respect the monad laws. But it doesn't matter, since I'm not a academician, I'm an engineer. So I'm using IO.catch : (unit -> 'a m) -> (exn -> 'a m) -> 'a m even for direct IO computations.