Browse thread
Value shadowing
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Brighten Godfrey <pbg@c...> |
| Subject: | Re: [Caml-list] Value shadowing |
On Aug 13, 2008, at 1:54 AM, David Allsopp wrote:
> [ 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 also find that style useful. Sometimes the type changes, but I can
recall useful cases where the type doesn't change, e.g., a sequence
of various operations on a list. Mock-up:
let lst = generate_list_somehow () in
let lst = List.filter (fun x -> x > 0) in
let lst = List.sort compare lst in
...
~Brighten