Stylianos Gakis
08/19/2022, 2:27 PMensureNotNull
exists as an extension function on EffectScope
Hm I tried to use the ensure function public suspend fun ensure(condition: Boolean, shift: () -> R)
inside the EffectScope
scope today and I was missing the nice functionality of the kotlin require
function which by using a contract allows the compiler to auto-infer the type after that function. Specifically when doing a require not null check in my case.
Since the ensure function does not do that, is there some other API I’m missing which could help me out, or should I just do !!
since I know myself at that point that the item is not null?Stylianos Gakis
08/19/2022, 2:28 PM@OptIn(ExperimentalContracts::class)
public suspend fun <R, B : Any> EffectScope<R>.ensureNotNull(value: B?, shift: () -> R): B {
contract { returns() implies (value != null) }
return value ?: shift(shift())
}
Silly me!simon.vergauwen
08/19/2022, 2:38 PMExperimentalContracts
requiring the fun to be final
.Youssef Shoaib [MOD]
08/19/2022, 8:56 PMB?.ensureNotNull(shift)
with the contract and everything exist for Arrow 2.0 with context receivers? It's not a "core" API per se, merely a convenience fun, so could such a fun exist?simon.vergauwen
08/22/2022, 8:13 AMcontext(EffectScope<R>)
public suspend fun <R, B : Any> ensureNotNull(value: B?, shift: () -> R): B {
contract { returns() implies (value != null) }
return value ?: shift(shift())
}
simon.vergauwen
08/22/2022, 8:13 AMA?
as a extension receiver globally is a bad idea, since it's valid for Any?
.Youssef Shoaib [MOD]
08/22/2022, 8:18 AMcontext(EffectScope<R>)
public suspend fun <R, B : Any> B?.ensureNotNull(shift: () -> R): B {
contract { returns() implies (value != null) }
return value ?: shift(shift())
}
For discoverability. I think it should not pollute the global autocomplete cuz of the context? Maybe IntelliJ would still autocomplete it idk.simon.vergauwen
08/22/2022, 8:20 AMensureNotNull
.
context(EffectScope<R>)
public suspend fun <R, B : Any> Either<R, B?>.ensureNotNull(shift: () -> R): B = TODO
simon.vergauwen
08/22/2022, 8:20 AMFor discoverability. I think it should not pollute the global autocomplete cuz of the context?
Youssef Shoaib [MOD]
08/22/2022, 8:24 AMNullable.zip
situation. Autocomplete is interesting because I think IntelliJ does show these functions in the global scope then complains about a missing context if you use it, but, I think, it pushes contextual functions (that don't have the required contexts in scope) down the list.simon.vergauwen
08/22/2022, 8:25 AM