Created
November 28, 2024 12:06
-
-
Save antonarhipov/2d75c2e6ed16cfa7876e96964306ead5 to your computer and use it in GitHub Desktop.
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
sealed interface Creature | |
data class Cthulhu( | |
val madnessLevel: Int, | |
val isAwakened: Boolean | |
) : Creature | |
data class Kraken( | |
val length: Double, | |
val isAggressive: Boolean | |
) : Creature | |
fun describeCreature(creature: Creature) = | |
when (creature) { | |
is Cthulhu if | |
creature.isAwakened -> "Cthulhu has awakened with madness level of ${creature.madnessLevel}!" | |
is Kraken if | |
creature.isAggressive -> "A ${creature.length}-meter long Kraken is attacking furiously!" | |
else -> "It's a cute ${creature::class.simpleName}" | |
} | |
fun main() { | |
val creatures: List<Creature> = listOf( | |
Cthulhu(madnessLevel = 9001, isAwakened = true), | |
Cthulhu(madnessLevel = 42, isAwakened = false), | |
Kraken(length = 20.5, isAggressive = true), | |
Kraken(length = 15.0, isAggressive = false) | |
) | |
creatures.forEach { creature -> | |
println(describeCreature(creature)) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment