Type literals in switch cases are confusing. #2911

@lrhn

Description

When you start switching over sealed type hierarchies, it's incredibly easy to write:

   case TheType: ....

when you mean to do a type check for TheType . Instead it compares to the Type object for the type literal TheType , and fails. With luck, you're told that the switch isn't exhaustive, but you still have to figure out why. (I'm guessing that we'll get warnings about unrelated types eventually, which will make it easier to spot the problem.)

I did this, as recently as yesterday.
Others do it too, like a stack-overflow question .

Very little switch-pattern code has been written, and it already crops up, so I fear it might be serious useabilty stumbler.

Maybe we should protect people against this by disallowing type literals as constant patterns without a leading const .
At least that will lead people to do either case TheType _: / case TheType(): or case const TheType: , depending on which one they want.

The big issue is that then existing switches over Type objects will need migration. I don't know how many such switches are, but probably non-zero.

(We could also change the meaning of case TheType: to be case TheType _: , but that risks being confusing in the other direction. If we just prevents the syntax now, it be possible to give it back later in either case.)