Skip to content

Instantly share code, notes, and snippets.

@bchazalet
Last active August 29, 2015 14:22
Show Gist options
  • Save bchazalet/cdfc87db697f7debd1df to your computer and use it in GitHub Desktop.
Save bchazalet/cdfc87db697f7debd1df to your computer and use it in GitHub Desktop.
Exhaustiveness warning for class with private constructor?
final class Currency private (val code: String) extends AnyVal
object Currency {
val USD: Currency = new Currency("USD")
val EUR: Currency = new Currency("EUR")
def from(code: String) : Option[Currency] = code match {
case USD.code => Option(USD)
case EUR.code => Option(EUR)
case _ => None
}
}
sealed abstract class Country(val code: String, val name: String)
object France extends Country("FR", "France")
object UnitedKingdom extends Country("UK", "United Kingdom")
object Country {
def from(code: String): Option[Country] = code match {
case France.code => Option(France)
case UnitedKingdom.code => Option(UnitedKingdom)
case _ => None
}
}
// TESTS
// we get an exhaustiveness warning
def test2(c: Country): String = c match {
case France => France.code
}
// we don't get an exhaustiveness warning
def test(c: Currency): String = c match {
case Currency.USD => Currency.USD.code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment