From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 7DEADBBAF for ; Mon, 31 May 2010 21:30:47 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhECABarA0xKfVI2mGdsb2JhbACeIAgVAQEBAQEICQwHESKvLYIChRsuiE8BAQMFhREEg0g X-IronPort-AV: E=Sophos;i="4.53,335,1272837600"; d="scan'208";a="60358696" Received: from mail-ww0-f54.google.com ([74.125.82.54]) by mail1-smtp-roc.national.inria.fr with ESMTP; 31 May 2010 21:30:47 +0200 Received: by wwb22 with SMTP id 22so461855wwb.27 for ; Mon, 31 May 2010 12:30:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:subject :to:cc:in-reply-to:references:mime-version:content-type:content-id; bh=Dubr9ZxybZvsJJNFV7BgUeuH29LeZGBEvubqpxtExKc=; b=htcI74sKXOwCg5f/5RcBQkfwXkw1iG9DdYq+CjXSrS4KGOZ3i/R1mKvwJeODAhONSe P8zgwxexxdWgmp/TqaC4rcqTbo7TaFxAxjutIG9doVJn8CGttUKEIefsd0Ud7s2UgLEG k3B278rPutDjQBbgIUCk7hKHE8K6edv0qZRFg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:subject:to:cc:in-reply-to:references :mime-version:content-type:content-id; b=u0CCVT9Q3XXGJerh4mflx6C9JO3S7uY0XdV5/5+DyXWM8rY6uIWMKKhtkQ5LmqPLuy 6NCnPXmcBGYrf6MgZCxze1DTbWER6JQiaRhkwdOCELXRiKza24tqt4TAS3WeLrX/DK7F HM4IgkdSxwX1vA+gAl8n04aRhfNWCuGq89tRg= Received: by 10.227.69.141 with SMTP id z13mr4612388wbi.46.1275334247190; Mon, 31 May 2010 12:30:47 -0700 (PDT) Received: from localhost (ks.feydakins.org [91.121.104.209]) by mx.google.com with ESMTPS id h22sm37389013wbh.21.2010.05.31.12.30.42 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 31 May 2010 12:30:42 -0700 (PDT) Message-ID: <4c040e62.9608e30a.4b34.6623@mx.google.com> Date: Mon, 31 May 2010 12:30:42 -0700 (PDT) From: Nicolas Pouillard Subject: Re: [Caml-list] Static exception analysis or alternative to using exceptions To: Goswin von Brederlow , Richard Jones Cc: caml-list@inria.fr In-Reply-To: <87y6f0cf4p.fsf@frosties.localdomain> References: <20100527170122.GA28273@annexia.org> <87y6f0cf4p.fsf@frosties.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <17516.1275334240.1@ks.feydakins.org> X-Spam: no; 0.00; 0200,:01 0200,:01 rafaelsen:01 monads:01 stdlib:01 complicates:01 typechecker:01 exn:01 exn:01 26,:98 imho:01 wrote:01 wrote:01 exception:01 exception:01 On Mon, 31 May 2010 16:36:22 +0200, Goswin von Brederlow wrote: > Richard Jones writes: > > > On Wed, May 26, 2010 at 06:15:05PM +0200, Hans Ole Rafaelsen wrote: > >> What experience does people have to using alternatives to exceptions, such > >> as option types or exception monads? Does use of third part libraries that > >> still throws exceptions make such approaches hard to use? Performance wise > >> it seems to be comparable to catching exceptions or matching for options, so > >> I guess the difference be might a question of programming style? > > > > Personally I've found that you should only throw those exceptions > > which can be caught in a single place in the program. By this I mean > > that an exception such as Not_found shouldn't be thrown, and instead > > it would be better to use an option type (for stdlib functions which > > throw Not_found, you have to be _very_ careful that the exception > > cannot "escape"). > > Which needlessly complicates your code when it never happens. > > Imho a good module should provide both an exception and option based > interface to fit the circumstances and programming style. 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 another. It tends to be easier to start from an option type in the case of Not_found instead of the other way around for the following reason: * The typechecker does not remind us to catch the exception. * We need a custom handler per exception. * We need to take a thunk to delay the computation. let not_found_to_option f = try Some (f ()) with Not_found -> None On the contrary look at: let from_option exn = function | Some x -> x | None -> raise exn Example: from_option Not_found (List.find p xs) Best regards, -- Nicolas Pouillard http://nicolaspouillard.fr