[Fixed] `ensureNotNull` exists as an extension fun...
# arrow
s
[Fixed]
ensureNotNull
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?
Ah this exists
Copy code
@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!
s
It needs to be an extension function because of
ExperimentalContracts
requiring the fun to be
final
.
y
Could a
B?.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?
s
I'm not sure what you mean at @Youssef Shoaib [MOD]?
Copy code
context(EffectScope<R>)
public suspend fun <R, B : Any> ensureNotNull(value: B?, shift: () -> R): B {
  contract { returns() implies (value != null) }
  return value ?: shift(shift())
}
Having
A?
as a extension receiver globally is a bad idea, since it's valid for
Any?
.
y
Copy code
context(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.
s
This function would conflict with for example. Meaning we cannot make any other extensions for the API name
ensureNotNull
.
Copy code
context(EffectScope<R>)
public suspend fun <R, B : Any> Either<R, B?>.ensureNotNull(shift: () -> R): B = TODO
For discoverability. I think it should not pollute the global autocomplete cuz of the context?
y
That makes sense yeah. Of course it will take up that name, similar to the
Nullable.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.
s
In that regard I think the extension function variant would behave better in face of auto-complete, no?