Created
March 7, 2018 12:06
-
-
Save tasaquino/0f8e22814bd0e2c40e7c08c453d2f0b7 to your computer and use it in GitHub Desktop.
Kotlin - Enum with any object example
This file contains 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
enum class Color { | |
RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET | |
} | |
fun mix(firstColor: Color, secondColor: Color) = | |
when (setOf(firstColor, secondColor)) { | |
setOf(Color.RED, Color.YELLOW) -> Color.ORANGE | |
setOf(Color.YELLOW, Color.BLUE) -> Color.GREEN | |
setOf(Color.BLUE, Color.VIOLET) -> Color.INDIGO | |
else -> throw Exception("bla color") | |
} | |
fun main(args: Array<String>) { | |
println(mix(Color.YELLOW, Color.BLUE)) | |
// GREEN | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment