-
-
Notifications
You must be signed in to change notification settings - Fork 23
Open
Description
Basically something the same as https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/with-default.html
Not sure if it would be as simple as below
class CacheWithDefault<K : Any, V : Any>(
val cache: Cache<K, V>,
private val default: (K) -> V?
) : Cache<K, V> by cache {
override fun get(key: K): V? = cache.get(key) ?: default(key)?.also { cache.put(key, it) }
}
fun <K : Any, V : Any> Cache<K, V>.withDefault(defaultValue: (K) -> V?): Cache<K, V> = when (this) {
is CacheWithDefault -> this.cache.withDefault(defaultValue)
is Cache -> CacheWithDefault(this, defaultValue)
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels