Skip to content

Instantly share code, notes, and snippets.

@Sintrastes
Created October 15, 2021 18:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sintrastes/dc09b2db881962767a160a3a9d484487 to your computer and use it in GitHub Desktop.
Save Sintrastes/dc09b2db881962767a160a3a9d484487 to your computer and use it in GitHub Desktop.
Example of how to emulate multiple recievers with bounded polymorphism in Kotlin
interface Logging {
fun log(message: String)
}
interface Prompt {
fun promptUser(): String
}
fun <Ctx> Ctx.exampleFn() where Ctx: Logging, Ctx: Prompt {
val message = promptUser()
log("Enter a message of length exactly 3:")
if (message.length == 3) {
log("Good!")
} else {
log("Not so good.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment