dave08
03/07/2024, 12:42 PMsuspend fun <T> invalidate(block: suspend () -> T = { Unit as T }): T =
cache.invalidate(key to emptyList(), block = block)
// when using this with the default block I need to provide type information... how can I omit it?
cache.invalidate<Unit>()
CLOVIS
03/07/2024, 1:03 PMsuspend fun <T> invalidate(block: suspend () -> T): T = …
suspend fun invalidate(): Unit = invalidate<Unit> {}
This is because { Unit as T }
makes no sense: the caller decides what to use as T
, so you cannot know if Unit
is a legal value of the type they choose.CLOVIS
03/07/2024, 1:04 PMcache.invalidate<Int>()
(spoiler: CCE)dave08
03/07/2024, 1:07 PMT
parameter and it returns Unit, that it would infer that T can only be Unit... but you're point is good -- since in the end the caller is supposed to decide...dave08
03/07/2024, 1:09 PMoverride suspend fun <R> invoke(
name: String,
type: KSerializer<R>,
vararg params: Any,
saveResultIf: (R) -> Boolean,
block: suspend () -> R
): R {
...
if (config?.nullPlaceholder != null && result == config.nullPlaceholder)
null as R
else
jsonParser.decodeFromString(type, result)
}
}
...
`null as R
dave08
03/07/2024, 1:11 PMCLOVIS
03/07/2024, 1:15 PM<R>
is a shorthand for <T : Any?>
, so null
is a legal value.CLOVIS
03/07/2024, 1:15 PMCLOVIS
03/07/2024, 1:16 PMInt
, so null
would be forbiddendave08
03/07/2024, 1:16 PMCLOVIS
03/07/2024, 1:17 PMR?
, so null
is always allowed even if the user used Int
dave08
03/07/2024, 1:19 PMfun someComputation(...): Foo = cache("some-cache") {
... this won't return a null, because it'll only cache computation results which will always not be null in this case
}
dave08
03/07/2024, 1:19 PMdave08
03/07/2024, 1:19 PMdave08
03/07/2024, 1:21 PMdave08
03/07/2024, 1:24 PMdave08
03/07/2024, 1:41 PM