This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.* | |
private val undefined = Any() | |
internal fun <T> Iterable<Flow<T>>.combineLatest(): Flow<List<T>> = | |
combineLatest { it } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dependencies: | |
// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9") | |
// implementation("org.jetbrains.kotlinx:atomicfu:0.14.4") | |
import kotlinx.atomicfu.* | |
import kotlinx.coroutines.* | |
import kotlinx.coroutines.channels.* | |
import kotlinx.coroutines.selects.* | |
import java.io.* | |
import kotlin.coroutines.* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@file:OptIn(ExperimentalTime::class) | |
import kotlinx.coroutines.* | |
import kotlin.time.* | |
suspend fun main() { | |
coroutineScope { | |
println("Launching tasks…") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
import kotlinx.coroutines.channels.* | |
import kotlinx.coroutines.flow.* | |
import kotlinx.coroutines.sync.* | |
import kotlin.coroutines.* | |
@Suppress("EXPERIMENTAL_API_USAGE") | |
suspend fun main() { | |
coroutineScope { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// same as Merge<Foo | Bar> | |
interface MergeOfFooAndBar { | |
a: number | |
b?: string | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Foo { | |
a: number; // no b | |
} | |
interface Bar { | |
a: number; b: string | |
} | |
function logB(value: Merge<Foo | Bar>) { | |
console.log(value.b) // .b is now an optional 'string' property |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Foo { | |
a: number; // no b | |
} | |
interface Bar { | |
a: number; b: string | |
} | |
function logB(value: Foo | Bar) { | |
console.log(value.b) // We cannot access .b as it isn't defined on Foo. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Merge<Union> = AddOptionalProperties<Union, keyof UnionToIntersection<Union>> | |
type AddOptionalProperties<Union, Properties extends keyof any> = | |
Union extends (infer Component) | |
? { [P in keyof Union]: Union[P] } & { [P in Exclude<Properties, keyof Union>]?: never } | |
: never | |
// https://stackoverflow.com/a/50375286/1183577 | |
type UnionToIntersection<Union> = | |
(Union extends any ? (_: Union) => void : never) extends ((_: infer Component) => void) ? Component : never |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.reflect.KProperty | |
import kotlin.reflect.KProperty0 | |
import kotlin.reflect.jvm.isAccessible | |
class SomeClass { | |
var someProperty by observable(1) | |
} |