caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Value shadowing
@ 2008-08-13  8:54 David Allsopp
  2008-08-13  9:15 ` [Caml-list] " Brighten Godfrey
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: David Allsopp @ 2008-08-13  8:54 UTC (permalink / raw)
  To: OCaml List

[ There's been at least one recent(ish) discussion on this - [1] ]

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

Now, the comments show what's supposed to happen in this function but that's
obviously not how it will behave because the entire list [xs] on line 1 is
shadowed by the tail of the list [xs] on line 3 so in both cases the tail of
the list will be returned. The type checker can do nothing as both have type
[bool list].

What would general opinion be if OCaml were to have a warning in this
instance - "[xs] on line 3 shadows [xs] on line 1 with the same type"?

As noted in the thread below, I too find

let x = _
in
  let x = _
  in
    ...

a useful style and would be greatly irritated by a warning on all shadowing
- but I think that in most cases for me the type of each [x] is different.
I've been stung a couple of times recently by non-contrived versions of the
function [foo] (through careless coding, of course - but that's what
warnings and type-checkers are about otherwise we'd all be using BCPL!)

Thoughts?


David


[1]
http://caml.inria.fr/pub/ml-archives/caml-list/2007/02/f60c7ea8cc0ebdbf9d1d5
6f0623b45a2.en.html


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2008-08-17 10:29 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-13  8:54 Value shadowing 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
2008-08-13 11:50 ` blue storm

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).