Created
November 30, 2018 14:40
-
-
Save oxbowlakes/d8d8cc47f0d90c49f3bdac94ccb3f193 to your computer and use it in GitHub Desktop.
Incorrect scalac warnings on aliased java enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Welcome to Scala 2.12.3 (OpenJDK 64-Bit Server VM, Java 1.8.0_181). | |
Type in expressions for evaluation. Or try :help. | |
scala> :paste | |
// Entering paste mode (ctrl-D to finish) | |
type TU = java.util.concurrent.TimeUnit | |
val NANOSECONDS: TU = java.util.concurrent.TimeUnit.NANOSECONDS | |
val MICROSECONDS: TU = java.util.concurrent.TimeUnit.MICROSECONDS | |
val MILLISECONDS: TU = java.util.concurrent.TimeUnit.MILLISECONDS | |
val SECONDS: TU = java.util.concurrent.TimeUnit.SECONDS | |
val MINUTES: TU = java.util.concurrent.TimeUnit.MINUTES | |
val HOURS: TU = java.util.concurrent.TimeUnit.HOURS | |
val DAYS: TU = java.util.concurrent.TimeUnit.DAYS | |
// Now define a match over all cases | |
def foo(x: TU): String = x match { | |
case NANOSECONDS => "ns" | |
case MICROSECONDS => "micros" | |
case MILLISECONDS => "ms" | |
case SECONDS => "s" | |
case MINUTES => "mins" | |
case HOURS => "hr" | |
case DAYS => "days" | |
} | |
// Exiting paste mode, now interpreting. | |
<console>:20: warning: match may not be exhaustive. | |
It would fail on the following inputs: DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, SECONDS | |
def foo(x: TU): String = x match { | |
^ | |
defined type alias TU | |
NANOSECONDS: TU = NANOSECONDS | |
MICROSECONDS: TU = MICROSECONDS | |
MILLISECONDS: TU = MILLISECONDS | |
SECONDS: TU = SECONDS | |
MINUTES: TU = MINUTES | |
HOURS: TU = HOURS | |
DAYS: TU = DAYS | |
foo: (x: TU)String | |
/* BUT! Non-aliased enums */ | |
Welcome to Scala 2.12.3 (OpenJDK 64-Bit Server VM, Java 1.8.0_181). | |
Type in expressions for evaluation. Or try :help. | |
scala> import java.util.concurrent.TimeUnit._ | |
import java.util.concurrent.TimeUnit._ | |
scala> :paste | |
// Entering paste mode (ctrl-D to finish) | |
def foo(x: java.util.concurrent.TimeUnit): String = x match { | |
case NANOSECONDS => "ns" | |
case MICROSECONDS => "micros" | |
case MILLISECONDS => "ms" | |
case SECONDS => "s" | |
case MINUTES => "mins" | |
case HOURS => "hr" | |
case DAYS => "days" | |
} | |
// Exiting paste mode, now interpreting. | |
foo: (x: java.util.concurrent.TimeUnit)String |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment