From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id AE2F1BBAF for ; Mon, 31 May 2010 22:57:44 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuIBAC/AA0zRVaE2mGdsb2JhbACeIggVAQEBAQEICQwHESKvHYIChSAuiE8BAQMFhREE X-IronPort-AV: E=Sophos;i="4.53,335,1272837600"; d="scan'208";a="60362971" Received: from mail-fx0-f54.google.com ([209.85.161.54]) by mail1-smtp-roc.national.inria.fr with ESMTP; 31 May 2010 22:57:44 +0200 Received: by fxm5 with SMTP id 5so2432706fxm.27 for ; Mon, 31 May 2010 13:57:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=iZ0YvmpyLk9haMk7mNrOnLjEW8s5cy/3UH3zqMvcqGg=; b=t+e7pRhb9SJVWcbSC8C7feuHqUvKCcO8U/2/B8TepEr2Q1bqQtiAGJBui9UIQlXVq5 BQ8fT5AlegYqPPtUj1C9evWWf5qbqtHAZlGlRhatX3hvuLuDBfjBseTOzQ4PL1U7p9fI dJzxjEl2LIwLUyRrTHwyH0JYVvS6U8BurwGj4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=B5DKyQADUGH/Rq8eUfw4td8uP89JH5vgYxJ1/rPAAMrzY5RsS1sf+eTl3gmxc3KoBq pned28hOVZnJLjEyWTa/8f1Kkovkdi2D4DgOYHuhDXhOzbNdEMhjzg/ycTzYCselWN6P +0o7iwb5JVvnb2demnIKrxYvI+Y7s7/S5VNfw= MIME-Version: 1.0 Received: by 10.102.182.28 with SMTP id e28mr2267582muf.78.1275339464147; Mon, 31 May 2010 13:57:44 -0700 (PDT) Received: by 10.204.26.195 with HTTP; Mon, 31 May 2010 13:57:44 -0700 (PDT) In-Reply-To: <4c040e62.9608e30a.4b34.6623@mx.google.com> References: <20100527170122.GA28273@annexia.org> <87y6f0cf4p.fsf@frosties.localdomain> <4c040e62.9608e30a.4b34.6623@mx.google.com> Date: Mon, 31 May 2010 22:57:44 +0200 Message-ID: Subject: Re: [Caml-list] Static exception analysis or alternative to using exceptions From: Lukasz Stafiniak To: Nicolas Pouillard Cc: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; lukasz:01 typechecker:01 exn:01 exn:01 syntax:01 higher-order:01 iterator:01 callee:01 wrote:01 exception:01 exception:01 caml-list:01 functions:01 functions:01 computation:01 On Mon, May 31, 2010 at 9:30 PM, Nicolas Pouillard wrote: > > Since having all functions in all flavours can lead to hard to interface > bloat, one should consider tiny functions to switch from a style to anoth= er. > It tends to be easier to start from an option type in the case of Not_fou= nd > instead of the other way around for the following reason: > > =A0* The typechecker does not remind us to catch the exception. > =A0* We need a custom handler per exception. > =A0* We need to take a thunk to delay the computation. > > =A0let not_found_to_option f =3D > =A0 =A0try Some (f ()) > =A0 =A0with Not_found -> None > > On the contrary look at: > > =A0let from_option exn =3D function > =A0 =A0| Some x -> x > =A0 =A0| None -> raise exn > > =A0Example: from_option Not_found (List.find p xs) > I use a syntax extension that catches "Not_found" and raises a failure instead, with the source location of the "real" offending call. I do this mostly because OUnit catches exceptions so backtraces are of no use. When I use the unmodified calls, I always handle Not_found right away, close to call point -- either by directly catching it, or by using a higher-order function, like an iterator that expects Not_found instead of None from the callee. Anyway, I vote for "option"ising all of the standard library and providing the function "unsome" raising a string with source code location of its call.