You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When creating a Result via err(x), the type of the value is not relevant until later - later being when the result is actually assigned to a variable of the Result type at which point the statically known type becomes dynamic.
It would be useful to be able to represent this "in-between" error value as being an error otherwise we always must know the full type at all times.
This becomes relevant in err itself that currently relies on type deduction of the result variable, a trick that only works in limited contexts.
A good example of this problem is or which cannot be combined with the self-deducing constructors:
procf(): Result[float, int] =let v =Result[int, int].ok(42)
# err deduces the "current" type to be `Result[int, int]` based on the # `result` variable rather than on the context in which it's being evaluated# (in this case `or` with the `v` variable: the result of this operation should# be a `Result[int, int]`)let x = v orerr(42)
Some ideas for how this could be solved:
err would use something else than result as a type source - how to inject that though?
err would return a magic type that converts to Result at the latest possible moment, automatically deducing T - since we know it's an error at this point, context can decide what T should be.
other solutions?
The text was updated successfully, but these errors were encountered:
typeErrMarker[E] =distinct E
templateErr[E](v: E): ErrMarker[E] =ErrMarker[E](v)
template`or`*[T, E0, E1](self: Result[T, E0], other: ErrMarker[E1]): Result[T, E1] =let s = (self) #TODO avoid copyif s.oResultPrivate:
whentype(self) istype(other):
s
else:
type R =Result[T, E1]
when T isvoid:
ok(R)
else:
ok(R, s.vResultPrivate)
else:
Result[T, E1].err(E1(other))
procf(): Result[float, int] =let v =Result[int, int].ok(42)
# err deduces the "current" type to be `Result[int, int]` based on the# `result` variable rather than on the context in which it's being evaluated# (in this case `or` with the `v` variable: the result of this operation should# be a `Result[int, int]`)let x = v orErr(42)
When creating a
Result
viaerr(x)
, the type of the value is not relevant until later - later being when the result is actually assigned to a variable of theResult
type at which point the statically known type becomes dynamic.It would be useful to be able to represent this "in-between" error value as being an error otherwise we always must know the full type at all times.
This becomes relevant in
err
itself that currently relies on type deduction of theresult
variable, a trick that only works in limited contexts.A good example of this problem is
or
which cannot be combined with the self-deducing constructors:Some ideas for how this could be solved:
err
would use something else thanresult
as a type source - how to inject that though?err
would return a magic type that converts toResult
at the latest possible moment, automatically deducing T - since we know it's an error at this point, context can decide what T should be.The text was updated successfully, but these errors were encountered: