Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: errors: Add generic errors.As #53045

Closed
Adimus11 opened this issue May 24, 2022 · 3 comments
Closed

proposal: errors: Add generic errors.As #53045

Adimus11 opened this issue May 24, 2022 · 3 comments

Comments

@Adimus11
Copy link

Adimus11 commented May 24, 2022

Hello!

Since Go 1.18 and introduced generics, method errors.As seems a little out of place as from API consumer it requires creating error variable along with if check, which cannot be done simply in one line, it even becomes more robust when more error assertions are done.

So general idea is to introduce in Golang with generics more general format of performing errors.As, today implemented something like this using generics to make API even more user friendly for one-lining it in if statements.

func ErrorAs[T error](source error) (T, bool) {
	var out T
	isErr := errors.As(source, &out)
	return out, isErr
}
@gopherbot gopherbot added this to the Proposal milestone May 24, 2022
@Adimus11 Adimus11 changed the title proposal: affected/package: errors proposal: errors: Add generic errors.As May 24, 2022
@seankhliao
Copy link
Member

Duplicate of #51945

@seankhliao seankhliao marked this as a duplicate of #51945 May 24, 2022
@go101
Copy link

go101 commented May 24, 2022

dup of #51945?
And my opinion: #51945 (comment)

@Adimus11
Copy link
Author

Duplicate of #51945

@Adimus11 Adimus11 closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2022
@golang golang locked and limited conversation to collaborators May 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants