From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q068YN3G029689 for ; Fri, 6 Jan 2012 09:34:23 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgoBAHqxBk9RZ90xkWdsb2JhbAApGqwKfyIBAQEBCQsLBxQDIoFyAQEBBDoPMBACAQgYChQQMiUCBAENDYd0BiO1bIsuYwSVB5I1 X-IronPort-AV: E=Sophos;i="4.71,467,1320620400"; d="scan'208";a="125802401" Received: from mtaout03-winn.ispmail.ntl.com ([81.103.221.49]) by mail4-smtp-sop.national.inria.fr with ESMTP; 06 Jan 2012 09:34:17 +0100 Received: from aamtaout04-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout03-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20120106083417.RASQ21018.mtaout03-winn.ispmail.ntl.com@aamtaout04-winn.ispmail.ntl.com>; Fri, 6 Jan 2012 08:34:17 +0000 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout04-winn.ispmail.ntl.com (InterMail vG.3.00.04.00 201-2196-133-20080908) with ESMTP id <20120106083417.CTUF23925.aamtaout04-winn.ispmail.ntl.com@romulus.metastack.com>; Fri, 6 Jan 2012 08:34:17 +0000 Received: from remus.metastack.local ([172.16.0.1]) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id q068YCjT003641 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL); Fri, 6 Jan 2012 08:34:13 GMT Received: from Remus.metastack.local ([fe80::547c:3c42:e1da:eda2]) by Remus.metastack.local ([fe80::547c:3c42:e1da:eda2%10]) with mapi id 14.01.0355.002; Fri, 6 Jan 2012 08:34:12 +0000 From: David Allsopp To: ivan chollet , "Richard W.M. Jones" CC: Lukasz Stafiniak , Diego Olivier Fernandez Pons , caml-list Thread-Topic: [Caml-list] Examples where let rec is undesirable Thread-Index: AQHMyZ8yIlHTrr7b9U2NFXE/rQHpApX5w5uAgARzrACAAAZZgIAAE1QAgAAb1wCAAJaWoA== Date: Fri, 6 Jan 2012 08:34:10 +0000 Message-ID: References: <20120105200442.GA17669@annexia.org> <20120105213636.GA30972@annexia.org> In-Reply-To: Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [212.183.128.40] Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.1 cv=JvdXmxIgLJv2/GthKqHpGJEEHukvLcvELVXUanXFreg= c=1 sm=0 a=QjxR3ELjTigA:10 a=e_pF8NIQzfcA:10 a=cTs9vV391PwA:10 a=kj9zAlcOel0A:10 a=xqWC_Br6kY4A:10 a=ZOzjf2MOAAAA:8 a=SGJpDYsEAAAA:8 a=4dnaRIAOAAAA:8 a=oxgFv6cpHm4Xen_AMKEA:9 a=CjuIK1q_8ugA:10 a=XIfrZ3bkEVgA:10 a=b0mGWxrnCzAA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id q068YN3G029689 Subject: RE: [Caml-list] Examples where let rec is undesirable ivan chollet wrote: > Sorry Richard I should have elaborated a bit more. > I guess there are a couple of examples in the literature, but one > of them comes to my mind, consider the following code snippet: > > let fd = Unix.open "myfile1" ... in > let fd = Unix.open "myfile2" ... in > ... (some code) > Unix.close fd I got stung by this kind of thing a few years ago (no resource leaking, just referring to a variable which had been shadowed and wasn't the string/list I thought it was - I'm capable of being very dim). I posted about it in 2008 - http://caml.inria.fr/pub/ml-archives/caml-list/2008/08/41f896b99ecf9a84b3f2e977bbc4e232.fr.html My own determination was that variable shadowing is fine as long as the type changes - because in most cases I found that errors were then caught by the type checker and the number of cases where I had to make variable names differ where I hadn't before was low enough. I've quickly chucked the scripts at www.metastack.com/ocaml/checkshadow.tar.gz (the camlp4 filter is Gabriel's: http://bluestorm.info/camlp4/dev/pf_shadow/list.php). The only problem is that the camlp4 filter doesn't handle [if] expressions correctly, but it works by saying (for foo.ml): ocamlfind ocamlopt -c -annot -package pf_shadow -syntax standard foo.ml && checkShadow foo [pf_shadow.cmo is built with ocamlfind ocamlc -syntax camlp4o -package camlp4.lib,camlp4.quotations -c pf_shadow.ml and checkShadow with ocamlfind ocamlopt -c -package unix checkShadow.ml] and emits warnings if it detects variable shadowing of the same type. I hasten to add, this was all done to deal with a personal deficiency - IMHO the language itself should not enforce this behaviour. I too find the complete ban on variable shadowing in other languages intensely irritating (especially when it's even between separate scopes within the same function) David