scalapuzzlers / scalapuzzlers.github.com

Github Pages behind scalapuzzlers.com
www.scalapuzzlers.com
161 stars 53 forks source link

matching on objects with lower-case first letters #154

Closed ryan-williams closed 4 years ago

ryan-williams commented 6 years ago
demobox commented 6 years ago

Thanks for submitting, @ryan-williams! Running this in a 2.12.2 REPL I get a bunch of warnings which rather give the game away :-(

scala> def test(o: obj) =
     |   o match {
     |     case one => 1
     |     case two => 2
     |   }
<console>:14: warning: patterns after a variable pattern cannot match (SLS 8.1.1
)
           case one => 1
                ^
<console>:15: warning: unreachable code due to variable pattern 'one' on line 14

           case two => 2
                       ^
<console>:15: warning: unreachable code
           case two => 2
                       ^
test: (o: obj)Int

Do you have any thoughts on ways to get around them?

ryan-williams commented 6 years ago

haha yea… I think I was running tests in IntelliJ and not seeing the compiler warnings when this bit me!

or I was ignoring compiler warnings due to a deluge of a pure expression does nothing in statement position warnings from tests of implicit derivations like these

Feel free to close if that is the protocol 😄 thanks!

demobox commented 6 years ago

Feel free to close if that is the protocol

There's no strict protocol as such, but it's generally nicer to "hide" something in such a way that the compiler doesn't point out the issue, if possible.

Of course, given the amount of warnings that the average project tends to produce, "hiding in plain sight" is very much a valid tactic in some sense ;-)

som-snytt commented 4 years ago

There ought to be a lint for bare case x =>, where it would not warn on case x @ _ =>. For unused pattern vars, it already distinguishes these two forms. The lint would trigger if there is an x in scope, much as missing interpolator is triggered "$x".

https://github.com/scala/bug/issues/11850

demobox commented 4 years ago

Closing this out for now - please re-open if we can come up with a suggestion on how to "hide" the warnings!