New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove warning about shadowed value names? #3375
Comments
I've never understood why this is a warning we want in the compiler vs a linter. Why do shadowed names indicate likely broken code or code that will break in the future (which is generally what our bar is for warnings)? |
I'm definitely in the enable-all-warnings camp, and would prefer to rename a shadowed variable rather than risk a bug. Giving users a choice about this via a compiler flag or linter option seems fair too. Do we have a linter that detects shadowing? |
You can end up with bugs because of disallowing shadowed warnings too - sometimes you reuse an Not having compiler options for toggling warnings is principle I'd like to stick with. Filtering them in your project's build is fine if you think you know better, but libraries should be required to resolve all of these warnings. Having them suppressable as part of the compiler interface will make it seem like ignoring them is okay. But that's also why this is a questionable warning: it's an opinion, rather than "you're definitely making a mistake, albeit a non-fatal one" like the other warnings are. |
I'm actually starting to be less in favour of this.
They don't necessarily, but I'm not sure that generally is our bar for warnings. For instance, I don't think any of these indicate likely broken code or code that will break in the future:
I feel like some warnings are more "things we are pretty confident ought to be addressed, for the sake of others reading this code later". To me, it doesn't quite feel consistent to reject a warning for shadowed names, but include one for unused variables.
At this point, |
Actually, come to think of it, if the basis of warnings is "things we are pretty confident ought to be addressed", maybe shadowed variable names don't make the cut, given that there are cases where we do in fact want to shadow variables (in order to prevent ourselves from referring to them again). |
I'm glad someone else said it. I didn't want to be the only one to dissent, but I'm against removing this warning at the moment. My reasoning is anecdotal: fewer issues exist if you cannot shadow a name than if you can. It's anecdotal because I haven't looked for actual research. If we're asking for vetoes, I'd like to veto removing this warning for now. |
Fair enough! I had been intending to come back today and restate my dislike of this warning, since all the times I've had to work around it recently came back to me when I was trying to sleep last night 😄 Shall I close it then? |
This and shadowing irrelevant values from the outer scope in a lambda cover ~95% of the time that I would want to use shadowing. In these cases adding a variable number of |
I'm still a fan of the shadowing warnings, but wanted to note for completeness that fixing shadowing puts a damper on using record pun shorthand: -- Concise with shadowing
foo1 :: Int -> Int
foo1 x = go {x, n: 7}
where
go {x, n: 0} = x
go {x, n} = go {x: x*x, n: n-1}
-- Noisy to fix shadowing
foo2 :: Int -> Int
foo2 x' = go {x:x', n: 7}
where
go {x, n: 0} = x
go {x, n} = go {x: x*x, n: n-1} Of course, there are lots of better ways to rewrite this arbitrary example. |
@milesfrain FWIW, and for anyone who may find it useful, the way I typically fix cases like that is to rewrite it as foo = \x -> ...
where
go { x, n } = ... |
A possible compromise is to allow variable shadowing in -- allowed
let
state = getState
state = doAThing state
state = doAnotherThing state
in
state
-- disallowed
strs # filter \str -> strs # some \str -> str /= str && isPrefix str str |
I feel like allowing shadowing in let state = getState in
let state = doAThing state in
let state = doAnotherThing state in
state As for |
If there's one place in PureScript syntax where we carve out an exception for this warning, it should be IMO we should absolutely not allow double-definition of a binding in a I'm fully neutral on continuing to warn on outer-to-inner shadowing, as with nested |
This seems like much more of an opinionated lint-y thing than the other warnings we throw out. There are definite valid reasons for wanting to shadow names - for example, often I want to hide an old version of a value in scope, or if #3245 is rejected, it enables an alternate workaround for nested
do
with differingbind
.It's hard to say for sure, but I think it has caused me more trouble than it has saved. Interested to hear other people's thoughts... pretty sure I know what @natefaubion thinks. 😉
The text was updated successfully, but these errors were encountered: