From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 4761C7F7C2 for ; Thu, 6 Feb 2014 12:36:31 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of yallop@gmail.com) identity=pra; client-ip=209.85.216.54; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="yallop@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of yallop@gmail.com designates 209.85.216.54 as permitted sender) identity=mailfrom; client-ip=209.85.216.54; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="yallop@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qa0-f54.google.com) identity=helo; client-ip=209.85.216.54; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="postmaster@mail-qa0-f54.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApcCAHJz81LRVdg2lGdsb2JhbABZg0RXgwG7b4ECCBYOAQEBAQcLCwkSKoIlAQEEASMEGQEbHQEDAQsGBQsNAgImAgIiAREBBQEcBhOHcAEDCQihF4wLU4MJk2MKGScNFU+IKREBBQyBHY1RB4JvgUkEmCuQNBgphFk8 X-IPAS-Result: ApcCAHJz81LRVdg2lGdsb2JhbABZg0RXgwG7b4ECCBYOAQEBAQcLCwkSKoIlAQEEASMEGQEbHQEDAQsGBQsNAgImAgIiAREBBQEcBhOHcAEDCQihF4wLU4MJk2MKGScNFU+IKREBBQyBHY1RB4JvgUkEmCuQNBgphFk8 X-IronPort-AV: E=Sophos;i="4.95,793,1384297200"; d="scan'208";a="57124074" Received: from mail-qa0-f54.google.com ([209.85.216.54]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 06 Feb 2014 12:36:30 +0100 Received: by mail-qa0-f54.google.com with SMTP id i13so2535856qae.13 for ; Thu, 06 Feb 2014 03:36:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=5UEvkcN1hf5SboAuZ2vGh18egJ4yF1w3wCIMgpPAj2c=; b=00+snnsJdccH8QlTlX0o5bHQ2jmmBD3re4nKJInAU2th6Itx/NM4PavBdAdWcU1vD7 xC5AWXVXaTZjmHB6V8iGfIpH70ZKp/doJyBG6+9kr+b3cY3Hx2c48PL+tT8Cdf+61JVw 2tfdJRozXPFiyPfcK38F7aNQltnupWJcJHm7YxR7hYgAs2UPPLe3pXsvIcTiQtB2HVxN mg+cgzb/njQKsF+n1pX3B0EhACMAIYcOhrqJ8R7OE4FcJb7QIlHboG9vB65ubzEwaCH+ sK+cyyUKOxwhV8Y5nVsinCLU+i6crcN4BXp/sfYmTWTYUNXUKCZDX+CkFbw9aQWYWcoN sy7w== MIME-Version: 1.0 X-Received: by 10.224.151.74 with SMTP id b10mr11312107qaw.68.1391686590016; Thu, 06 Feb 2014 03:36:30 -0800 (PST) Received: by 10.229.131.65 with HTTP; Thu, 6 Feb 2014 03:36:29 -0800 (PST) In-Reply-To: <20140206105811.GD28534@frosties> References: <20140206105811.GD28534@frosties> Date: Thu, 6 Feb 2014 11:36:29 +0000 Message-ID: From: Jeremy Yallop To: Goswin von Brederlow Cc: Caml List Content-Type: text/plain; charset=UTF-8 Subject: Re: [Caml-list] Proposal: extend try to handle success On 6 February 2014 10:58, Goswin von Brederlow wrote: > Examples from the blog: [...] > let rec iter_stream f s = > try MyStream.get s > with > | End_of_stream -> () > | val (x, s') -> > f x; > iter_stream f s' > > Isn't that the same as this? > > exception Success of (char * MyStream.t) > let rec iter_stream f s = > try raise (Success (MyStream.get s)) > with > | End_of_stream -> () > | Success (x, s') -> > f x; > iter_stream f s' Yes, it's essentially the same if your streams are monomorphic. However, if MyStream.t has a type parameter then you'll need something more like this: let rec iter_stream : type a. (a -> unit) -> a MyStream.t -> unit = fun f s -> let module M = struct exception Success of (a * a MyStream.t) end in try raise (M.Success (MyStream.get s)) with | End_of_stream -> () | M.Success (x, s') -> f x; iter_stream f s' The code becomes quite cluttered with irrelevant details, but things can get even worse. In some cases there's no way to write down the type of the expression at all, which makes defining the exception rather tricky. > And what does this do? > > let f1 x = > try None > with > | Empty -> () > | val Some x -> x > | val _ -> 1 > > let f2 x = > try raise Not_found > with > | Empty -> () > | val Some x -> x > | val _ -> 1 > > let f3 x = > try raise Not_found > with > | Empty -> () > | val Some x -> x > | _ -> 1 > > let f4 x = > try None > with > | Empty -> () > | val Some x -> x > | _ -> 1 > > My guesses: > - f1 returns 1 > - f2 throws Not_found > - f3 returns 1 > - f4 returns 1 Change '()' to '0' and your first three guesses are correct. The last function behaves as follows: # #use "f4.ml";; File "f4.ml", line 2, characters 2-63: Warning 8: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: None val f4 : 'a -> int = # f4 ();; Exception: Match_failure ("f4.ml", 2, 2). > Or does "_" only match exceptions and "val _" any value? Right. The behaviour of '_' is unchanged: it matches any exception. The pattern 'val _' matches any value.