caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Mauricio Fernandez <mfp@acm.org>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Value shadowing
Date: Wed, 13 Aug 2008 17:03:26 +0200	[thread overview]
Message-ID: <20080813150326.GI6952@NANA.localdomain> (raw)
In-Reply-To: <C33B6B8CD9C44358A74F323324EA0FCA@countertenor>

On Wed, Aug 13, 2008 at 12:04:21PM +0100, David Allsopp wrote:
> > On Wed, Aug 13, 2008 at 09:54:36AM +0100, David Allsopp wrote:
> > > Suppose I have this piece of code:
> > > 
> > > let foo xs =
> > >   match xs with
> > >     x::xs -> if x
> > >              then xs (* Return the tail of the list *)
> > >              else xs (* Return the entire list *)
> > >   | []    -> raise Exit
> >
> > I'd find it very counter-intuitive if OCaml behaved like this, and
> > annoying if it gave a warning.  Just name the variables to be
> > different!
> 
> You seem to have missed my point that I wrote the above *in error* so "Just
> name the variables to be different!" is a clairvoyant response in this
> case... I'm aware that that's what has to change :o)

(Somewhat tangentially)

I often reuse variable names *in order to avoid errors*, e.g., I prefer
  let l = foo l in
to
  let l' = foo l in
when I have no need for the original l in subsequent code and both l and l'
have the same type. This prevents a potential bug (using l instead of l'
later).

Shadowing is useful when you could code in point-free style but decide to name
the intermediate values for clarity.

-- 
Mauricio Fernandez  -   http://eigenclass.org


  parent reply	other threads:[~2008-08-13 15:05 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-13  8:54 David Allsopp
2008-08-13  9:15 ` [Caml-list] " Brighten Godfrey
2008-08-13  9:56   ` David Allsopp
2008-08-13 10:49     ` [Caml-list] Value shadowing (tangent) Brighten Godfrey
2008-08-13 11:04       ` David Allsopp
2008-08-13 11:04       ` Brighten Godfrey
2008-08-13 11:17         ` Daniel Bünzli
2008-08-13 23:05           ` Stefano Zacchiroli
2008-08-13 23:33             ` Daniel Bünzli
2008-08-13 11:05       ` Vincent Hanquez
2008-08-16 20:02         ` Pierre Etchemaïté
2008-08-17  8:07           ` David Allsopp
2008-08-17 10:28             ` Pierre Etchemaïté
2008-08-13 10:33   ` [Caml-list] Value shadowing Jim Farrand
2008-08-13 10:12 ` Richard Jones
2008-08-13 11:04   ` David Allsopp
2008-08-13 12:26     ` blue storm
2008-08-13 15:03     ` Mauricio Fernandez [this message]
2008-08-13 11:50 ` blue storm

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=20080813150326.GI6952@NANA.localdomain \
    --to=mfp@acm.org \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).