From: David Allsopp <dra-news@metastack.com>
To: ivan chollet <ivan.chollet@gmail.com>,
"Richard W.M. Jones" <rich@annexia.org>
Cc: Lukasz Stafiniak <lukstafi@gmail.com>,
Diego Olivier Fernandez Pons <dofp.ocaml@gmail.com>,
caml-list <caml-list@inria.fr>
Subject: RE: [Caml-list] Examples where let rec is undesirable
Date: Fri, 6 Jan 2012 08:34:10 +0000 [thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D9C2616A7D@Remus.metastack.local> (raw)
In-Reply-To: <CACm_MF-hxbJEo=g4wNgN-YmHE+EeQzoUiDCMqEszOOgoOJfsdw@mail.gmail.com>
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
next prev parent reply other threads:[~2012-01-06 8:34 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-01-02 22:37 Diego Olivier Fernandez Pons
2012-01-02 22:49 ` Alexandre Pilkiewicz
2012-01-03 0:05 ` Lukasz Stafiniak
2012-01-03 5:47 ` Martin Jambon
2012-01-03 8:07 ` Gabriel Scherer
2012-01-05 20:04 ` Richard W.M. Jones
2012-01-05 20:27 ` ivan chollet
2012-01-05 20:46 ` Gabriel Scherer
2012-01-05 21:39 ` Richard W.M. Jones
2012-01-06 2:39 ` Cedric Cellier
2012-01-06 15:22 ` Damien Doligez
2012-01-05 21:36 ` Richard W.M. Jones
2012-01-05 23:16 ` ivan chollet
2012-01-06 8:34 ` David Allsopp [this message]
2012-01-06 10:34 ` Daniel Bünzli
2012-01-03 13:05 ` Yaron Minsky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=E51C5B015DBD1348A1D85763337FB6D9C2616A7D@Remus.metastack.local \
--to=dra-news@metastack.com \
--cc=caml-list@inria.fr \
--cc=dofp.ocaml@gmail.com \
--cc=ivan.chollet@gmail.com \
--cc=lukstafi@gmail.com \
--cc=rich@annexia.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox