Skip to content

Instantly share code, notes, and snippets.

@evertheylen
Created April 9, 2018 21:36
Show Gist options
  • Save evertheylen/026806a0fcb7844db766884dbfd7deed to your computer and use it in GitHub Desktop.
Save evertheylen/026806a0fcb7844db766884dbfd7deed to your computer and use it in GitHub Desktop.
// Some (Java) library:
class Context {
fun register(cl: SomeClient) {
cl.initClient(this)
}
}
abstract class SomeClient {
abstract fun initClient(ctx: Context)
}
// Own program:
class OwnClient: SomeClient() {
var context: Context? = null
override fun initClient(ctx: Context) {
context = ctx
}
fun ownCode() {
// Better way of handling context?
if (context == null) {
// handle this...
} else {
// can't just use context, since it is a var, have to use context!! everywhere :(
println("hello world")
}
}
}
fun main(args: Array<String>) {
val ctx = Context()
var cl = OwnClient()
ctx.register(cl)
cl.ownCode()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment