Open mattpocock opened 1 month ago
Since current compiler's type narrowing and control flow analysis do not work well with yield*
, I think safeTry
must support returning Err, not only yield*
ing.
For example, yield* err(something).safeUnwrap()
never returns, but the compiler doesn't treat it well.
declare const x: 1 | 2
function* g() {
if (x === 1) {
yield* err("XXX").safeUnwrap();
}
x // still typed as `1 | 2`
}
So, we need to use return to exit from it.
declare const x: 1 | 2
function* g() {
if (x === 1) {
return err("XXX");
}
x // typed as `2`, as expected
}
It is common behavior for Generator<T, never>
, not specific for safeUnwrap.
https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEYD2A7AzgF3gDwFzwEZ4AfeAJgCgKAzAVxTAwEtUAqeAcwIAoBKAbwrxh8JtXjds8ALyzhBXvEEiVATyYgIwdiBgwAylGogAqigDuMKAAduCgNxCRAXyoqpAeg+ES5Cq5p6RhYUdg4yPmURMQkpWWl5RSiVYTgMWhgUeF0DI1MLK1sHJ2EA93gvPwDQSFgEZHQsHMNjM0sbfAAeABUAPkl8bsVpXvgAcRAUXSgMJBgegBp4KYA3XV6gA
So, for now, I don't think it's a good idea to wrap the returned value automatically.
@tsuburin Agree, this makes sense as a 1-2 punch. Has this been raised as a separate issue?
@tsuburin I meant in this repo, since returning err()
from safeTry
makes sense as its own feature.
@mattpocock Oh, sorry. As far as I know, it hasn't.
Problem
One awkward thing about
.safeTry
is that it requires you to wrap everything returned in aResult
.This gets particularly awkward when
safeTry
is modelling a function that returnsvoid
.Solution
I propose that
safeTry
should automatically wrap returned values withok
.This would make
safeTry
returningvoid
perfectly fine:Existing
safeTry
functions returningok
orerr
would still be respected.This matches the behaviour found in Effect.gen.