Created
May 22, 2017 11:12
-
-
Save Supuhstar/55cc355c4db452845d86a73f0d8d2e9c to your computer and use it in GitHub Desktop.
Basic Kotlin
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
sealed class Topping { | |
class Pepperoni: Topping() | |
class Cheese(val type: CheeseType, val extra: Boolean): Topping() | |
class Sausage(val thickness: Double): Topping() | |
} | |
fun Topping.toString(): String { | |
return when (this) { | |
is Cheese -> "${extra ? "extra " : ""}$type cheese" | |
is Pepperoni -> "pepperoni" | |
is Sausage -> "$thickness-inch thick sausage" | |
} | |
} | |
enum class CheeseType { | |
mozzarella, feta, four | |
} | |
data class Pizza(val toppings: List<Topping>) { | |
override fun toString(): String { | |
return "A pizza with " + toppings.reduce { description, topping -> | |
(description ?: "") + topping.toString() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment