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 Error // implementations in same package and module only |
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 class Mammal(val name: String) | |
| class Cat(val catName: String) : Mammal(catName) | |
| class Human(val humanName: String, val job: String) : Mammal(humanName) | |
| fun greetMammal(mammal: Mammal): String { | |
| return when (mammal) { | |
| is Human -> "Hello ${mammal.name}; You're working as a ${mammal.job}" | |
| is Cat -> "Hello ${mammal.name}" | |
| } | |
| } |
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
| fun greetMammal(mammal: Mammal): String { | |
| return when (mammal) { | |
| is Human -> "Hello ${mammal.name}; You're working as a ${mammal.job}" | |
| is Cat -> "Hello ${mammal.name}" | |
| else -> "Hello unknown creature" | |
| } | |
| } |
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
| // This does not compile because the `when` is not exhaustive | |
| fun greetMammal(mammal: Mammal): String { | |
| return when (mammal) { | |
| is Human -> "Hello ${mammal.name}; You're working as a ${mammal.job}" | |
| is Cat -> "Hello ${mammal.name}" | |
| } | |
| } |
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
| open class Mammal(val name: String) | |
| class Cat(val catName: String) : Mammal(catName) | |
| class Human(val humanName: String, val job: String) : Mammal(humanName) |
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
| context(PrintingScope, TimeScope) | |
| fun <K, V> Map<K, V>.customPrint() { | |
| // ... | |
| } | |
| // get type in two different ways | |
| val funType1: KFunction3<PrintingScope, TimeScope, Map<String, String>, Unit> = | |
| Map<String, String>::customPrint |
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
| val mapping = mapOf("a" to 1, "b" to 2, "c" to 3) | |
| with(PrintingScope()) { | |
| with(TimeScope()) { | |
| mapping.customPrint() | |
| } | |
| } |
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
| class PrintingScope( | |
| val separator: String = "________________" | |
| ) | |
| class TimeScope { | |
| fun getCurrentTime(): LocalTime = | |
| LocalDateTime.now().toLocalTime().truncatedTo(ChronoUnit.SECONDS) | |
| } | |
| context(PrintingScope, TimeScope) |
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
| class PrintingScope( | |
| val separator: String = "________________" | |
| ) | |
| context(PrintingScope) | |
| fun <K, V> Map<K, V>.customPrint() { | |
| forEach { (k, v) -> | |
| println("K: $k") | |
| println("V: $v") | |
| println(separator) |
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
| val mapping = mapOf("a" to 1, "b" to 2, "c" to 3) | |
| val customizer = PrintingCustomizer() | |
| // usage | |
| with(customizer){ | |
| mapping.customPrint() | |
| } |
NewerOlder