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:
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.
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:
https://try.purescript.org/?gist=cd41b818f5853806f836489993e523fb
and expect it to print:
when it actually prints:
The root cause is a misunderstanding of how pattern matching works with
case. In this example, incorrectly assuming a match against the value ofv, rather than the actual behavior of matching allJusts and overwritingv.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.