Last active
August 29, 2015 14:22
-
-
Save bchazalet/cdfc87db697f7debd1df to your computer and use it in GitHub Desktop.
Exhaustiveness warning for class with private constructor?
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
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