Skip to content

Instantly share code, notes, and snippets.

@ReneeVandervelde
Last active June 11, 2017 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReneeVandervelde/23c3df246763cd68d1d64c0087e9f822 to your computer and use it in GitHub Desktop.
Save ReneeVandervelde/23c3df246763cd68d1d64c0087e9f822 to your computer and use it in GitHub Desktop.
interface KotlinConsumer<T> {
fun accept(value: T)
}
class KotlinObservable<T> {
fun subscribe(onNext: KotlinConsumer<T>) {}
}
class Test {
fun main() {
// FAILS: Type Mismatch: inferred type is KFunction1<@ParameterName String, Unit> but KotlinConsumer<String> was expected
KotlinObservable<String>().subscribe(this::stringConsumer)
// FAILS: Type mismatch: inferred type is () -> ??? but KotlinConsumer<String> was expected
KotlinObservable<String>().subscribe({ println(it) })
// Works: Anonymous Implementation of the class
KotlinObservable<String>().subscribe(object: KotlinConsumer<String> {
override fun accept(value: String) { println(value) }
})
}
// I want to use this method, but can't
fun stringConsumer(value: String) {
println(value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment