Version française
Home     About     Download     Resources     Contact us    
Browse thread
Value shadowing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Mauricio Fernandez <mfp@a...>
Subject: Re: [Caml-list] Value shadowing
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