phldavies
10/25/2024, 12:45 PMResource<A> with a .use { } that required a non-local return (and thus required use to be inline)
Can anyone see any issue with taking the following approach here:
@OptIn(DelicateCoroutinesApi::class)
suspend inline fun <A, R> Resource<A>.useInline(block: (A) -> R): R {
val (resource, release) = allocate()
try {
val result = block(resource)
release(ExitCase.Completed)
return result
} catch (ex: Throwable) {
release(ExitCase.ExitCase(ex))
throw ex
}
}
Also, in arrow1.x we had allocated (deprecated) and allocate (the safer and recommended version) but in Arrow2, allocate was renamed back to allocated despite allocate feeling semantically more correct - was there any specific reason for this?Youssef Shoaib [MOD]
10/25/2024, 12:49 PMExitCase function handles that). use should ideally be inline. I had a WIP PR that revamped the ResourceScope interface and made use be inline. I'll see if I can clean that up.
Looking at the code, resourceScope should just be inline, not sure why it isn't. use should thus be inline too.Youssef Shoaib [MOD]
10/25/2024, 12:55 PMrelease does the throwing automatically btw so no need to rethrowphldavies
10/25/2024, 12:55 PMresourceScope and Resource<A>.use inline for Arrow2Youssef Shoaib [MOD]
10/25/2024, 12:56 PMmerge or impure as your "non-local return"phldavies
10/25/2024, 1:04 PMthrow as the compiler doesn't know that ExitCase.ExitCase always returns Cancelled | Failure and that release returns Nothing in both cases - despite it never being reached.Youssef Shoaib [MOD]
10/25/2024, 1:20 PMphldavies
10/25/2024, 1:41 PMYoussef Shoaib [MOD]
10/25/2024, 3:32 PM@PublishedApi FYI, but that should be fine!phldavies
10/25/2024, 4:39 PMResource.allocate approach to avoid exposing the entire ResourceScopeImpl class in the ABI to hopefully limit any impact of future refactors