Skip to content

Instantly share code, notes, and snippets.

@Supuhstar
Created May 22, 2017 11:12
Show Gist options
  • Save Supuhstar/55cc355c4db452845d86a73f0d8d2e9c to your computer and use it in GitHub Desktop.
Save Supuhstar/55cc355c4db452845d86a73f0d8d2e9c to your computer and use it in GitHub Desktop.
Basic Kotlin
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