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
I propose small change to errors.As function.
To use type constraint for the second parameter. func As[E error](err error, target *E) bool
What is the motivation?
Move a potential error from runtime to build time, even though it's being reported by go vet
second argument to errors.As must be a non-nil pointer to either a type that implements error, or to any interface type
Current any says nothing, the real constraint is described in documentation.
As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.
Is is backward compatible change?
Yes and no.
In code, which doesn't follow documentation, it can lead to uncompilable code.
It has potential to crash in runtime, and is false in general.
I propose small change to
errors.As
function.To use type constraint for the second parameter.
func As[E error](err error, target *E) bool
What is the motivation?
Move a potential error from runtime to build time, even though it's being reported by
go vet
Current
any
says nothing, the real constraint is described in documentation.Is is backward compatible change?
Yes and no.
In code, which doesn't follow documentation, it can lead to uncompilable code.
It has potential to crash in runtime, and is false in general.
Example:
https://go.dev/play/p/fhBlYngn_fQ
The synergy of
errors.As
and type constraints was already mentioned in early days of generics proposal.https://go.googlesource.com/proposal/+/master/design/go2draft-error-inspection.md#The-Is-and-As-Functions
The text was updated successfully, but these errors were encountered: