Skip to content

Proposal: Enable shadowing warnings in case patterns #3880

@milesfrain

Description

@milesfrain

The compiler can help beginners avoid the following mistake by enabling warnings for shadowed variables in patterns of case expressions.

The naive user may write the following:

showVal :: Maybe Int -> Int -> String
showVal special v =
  let
    showV = show v
  in
    case special of
      Just v -> showV <> " is special"
      _ -> showV

main :: Effect Unit
main = render =<< withConsole do
  traverse_ log $ map (showVal $ Just 2) $ 1..3

https://try.purescript.org/?gist=cd41b818f5853806f836489993e523fb

and expect it to print:

1
2 is special
3

when it actually prints:

1 is special
2 is special
3 is special

The root cause is a misunderstanding of how pattern matching works with case. In this example, incorrectly assuming a match against the value of v, rather than the actual behavior of matching all Justs and overwriting v.

Shadowing warnings would help highlight this misunderstanding.

If the shadowing warnings are added, then would it be possible to support pattern matching against the value of v? This could be a nice feature, as it can result in cleaner syntax than if using guards.

There's another proposal to completely remove shadowing warnings in #3375, which is incompatible with this one.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions