Skip to content

Instantly share code, notes, and snippets.

View arkivanov's full-sized avatar

Arkadii Ivanov arkivanov

View GitHub Profile
@arkivanov
arkivanov / java_invariance_in_kotlin_1.kt
Created May 10, 2019 15:52
Java invariance in Kotlin 1
fun bar(source: Observable<CharSequence>) {
}
fun foo(source: Observable<String>) {
bar(source) // Compilation error
}
@arkivanov
arkivanov / java_invariance_in_kotlin_2.kt
Created May 10, 2019 15:53
Java invariance in Kotlin 2
fun bar(source: Observable<out CharSequence>) {
source.scan { a, b -> "$a,$b" } // Compilation error
}
fun foo(source: Observable<String>) {
bar(source)
}
@arkivanov
arkivanov / kotlin_producer_interface.kt
Created May 10, 2019 15:55
Kotlin producer interface
interface Observable<out T> {
}
@arkivanov
arkivanov / java_invariance_in_kotlin_3.kt
Created May 10, 2019 15:56
Java invariance in Kotlin 3
fun bar(consumer: Consumer<String>) {
}
fun foo(consumer: Consumer<CharSequence>) {
bar(consumer) // Compilation error
}
@arkivanov
arkivanov / java_invariance_in_kotlin_4.kt
Created May 10, 2019 15:57
Java invariance in Kotlin 4
fun bar(consumer: Consumer<in String>) {
if (consumer is Subject) {
val value: String = consumer.value // Compilation error
}
}
fun foo(consumer: Consumer<CharSequence>) {
bar(consumer)
}
@arkivanov
arkivanov / kotlin_consumer_interface.kt
Created May 10, 2019 15:57
Kotlin consumer interface
interface Consumer<in T> {
fun accept(value: T)
}
@arkivanov
arkivanov / reaktive_variance_example_1.kt
Created May 10, 2019 15:58
Reaktive variance example 1
fun foo() {
val source: Observable<String> = observableOf("Hello")
bar(source) // No error
}
fun bar(source: Observable<CharSequence>) {
}
@arkivanov
arkivanov / simple_java_interface_with_rxjava_single.java
Last active May 10, 2019 16:04
Simple Java interface with RxJava Single
public interface UserDataSource {
Single<User> load();
}
@arkivanov
arkivanov / reaktive_null_safety_examples.kt
Last active May 10, 2019 16:13
Reaktive null-safety exmaples
// 1
observableOf<String>(null) // Compilation error
// 2
val o1: Observable<String?> = observableOf(null)
val o2: Observable<String> = o1 // Compilation error, types do not match
// 3
val o1: Observable<String?> = observableOf(null)
val o2: Observable<String> = o1.notNull() // No error, null values filtered out
@arkivanov
arkivanov / Fragments.kt
Last active February 15, 2020 07:30
Fragment tree with inputs and outputs
class FragmentWithDialog(output: (Output) -> Unit) : Fragment() {
sealed class Output {
object DialogShown : Output()
}
}
class FragmentWithInput(input: Observable<Input>) : Fragment() {
sealed class Input {