Skip to content

Instantly share code, notes, and snippets.

Avatar
🌻
Keeping software in full bloom

Renee Vandervelde ReneeVandervelde

🌻
Keeping software in full bloom
View GitHub Profile
@ReneeVandervelde
ReneeVandervelde / UserInfo.kt
Created Jan 30, 2019
Simple example of fetching info from an Android SDK class
View UserInfo.kt
class UserInfoFormatter(
private val resources: Resources
) {
fun getDisplayName(user: User) {
if (user.name.isNullOrEmpty()) {
return resources.getString(R.string.name_unknown)
} else {
return user.name
}
}
View undefinedNewParameter.kt
@Deprecated(
message =Tell me what Foo is!”,
replaceWith = ReplaceWith(“newHotness(foo, bar)”)
)
fun oldAndBusted(bar: String) {}
View addingParameters.kt
@Deprecated(
message = “I forgot about Foo!”,
replaceWith = ReplaceWith(“newHotness(DEFAULT, bar)”, “acme.sandbox.foo.DEFAULT”)
)
fun oldAndBusted(bar: String) {}
View parameterOrder.kt
@Deprecated(
message = “I wrote this backwards”,
replaceWith = ReplaceWith(“newHotness(foo, bar)”)
)
fun oldAndBusted(bar: String, foo: String) {}
View replacewith.kt
@Deprecated(
message =Use something different”,
replaceWith = ReplaceWith(“newHotness()”)
)
fun oldAndBusted() {}
View works_wtf.kt
import io.reactivex.Observable
import io.reactivex.functions.Consumer
class Foo {
fun main() {
Observable.just("foo").subscribe(this::stringConsumer, this::errorConsumer)
}
fun stringConsumer(value: String) { TODO() }
fun errorConsumer(error: Throwable) { TODO() }
View Failure.kt
import io.reactivex.Observable
import io.reactivex.functions.Consumer
class MyErrorHandler : Consumer<Throwable> {
override fun accept(t: Throwable?) { TODO() }
}
class Foo {
fun main() {
val errorHandler = MyErrorHandler()
View kotlinObservables.kt
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
View SAM.kt
import io.reactivex.Observable
import io.reactivex.functions.Consumer
class Foo {
fun main() {
Observable.just("foo", "bar").subscribe(this::stringConsumer)
}
fun stringConsumer(value: String) {
println(value)
View test.kt
fun setOnClickListener(listener: (view: View) -> Unit) {
// ...
}
fun main() {
setOnClickListener { it.bringToFront() }
setOnClickListener(this::test)
}
fun test(view: View) {