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: spec: add zero-inequality operator ? #71320

Closed
bdbyrne opened this issue Jan 18, 2025 · 5 comments
Closed

proposal: spec: add zero-inequality operator ? #71320

bdbyrne opened this issue Jan 18, 2025 · 5 comments
Labels
error-handling Language & library change proposals that are about error handling. LanguageChange Suggested changes to the Go language LanguageProposal Issues describing a requested change to the Go language specification. Proposal
Milestone

Comments

@bdbyrne
Copy link

bdbyrne commented Jan 18, 2025

Proposal Details

Background

The Go community has a wealth of opinions on error handling: some are indifferent, some are annoyed, and some want to watch the world burn (#71203[1]). The goal of this modest proposal[2] is to surface a common middle-ground to see if there's any light to be found.

This is a modern generalization of #32845, and aside from the existing error handling proposals, relates to #64825.

Proposal

A new post-fix operator ? evaluates for inequality with the zero value for comparables. For exposition, the following are equivalent:

// error
if err != nil {...}
if err? {...}

// error (2)
if _, err := f(); err != nil {...}
if _, err := f(); err? {...}

// error (3)
ok := err != nil
ok := err?

// *T
if ptr != nil {...}
if ptr? {...}

// int
if value != 0 {...}
if value? {...}

// bool
if value {...}
if value? {...} // OK, but likely vetted

The benefits include:

  • Saves six characters[3][4].
  • Arguably easier to type on QWERTY keyboards.
  • There's no == zero equivalent, so it make those if-statements stand out more.

Sugar

Behavior can be emulated today with:

func nonzero[T comparable](value T) bool {
	var zero T
	return value != zero
}
// Sugar: 'x?' is rewritten as 'nonzero(x)'.

Unconventional cases that should be vetted:

true?/false?
!value?
value??
&value?  // Compiler error

[1] Said 100% in jest.
[2] But seriously.
[3] I don't type the spaces and let gofmt add those - OK, four characters.
[4] My editor auto-completes and I just hit tab - OK, fair enough.

@gopherbot gopherbot added this to the Proposal milestone Jan 18, 2025
@mateusz834
Copy link
Member

A new post-fix operator ? evaluates for inequality with the zero value on comparable types except bool. For exposition, the following are equivalent:

I don't think the except bool serves anybody, also this kind of ? operator should work with all types, not only comparable, see #61372.

@gabyhelp gabyhelp added the LanguageProposal Issues describing a requested change to the Go language specification. label Jan 18, 2025
@jimmyfrasche
Copy link
Member

This (extended to all zeroes, maybe a vet check for bools) + #21182 would handle a lot of the most common rough spots around zero values and it would be nice to only have to read

if err? {
  return ..., fmt.Errorf("blah: %w", err)
}

While that simplifies the boilerplate around the common cases of returning an error as-is or reacting to/decorating the error and returning, it's still 3 lines to read even if the important parts are more in focus, so I'm not convinced this is a better idea than something that (somehow) does just those 2 things on the same line.

Checking if something is zero looks a bit busy:!something?

@bdbyrne
Copy link
Author

bdbyrne commented Jan 18, 2025

I don't think the except bool serves anybody

You're right, there's no major gotchas and vet can help identify unnecessary usage. I've edited the proposal.

I must admit, with the edits in place, it becomes much more similar to past proposals and so may not be worth rehashing. The only difference would be time, as the community seems more open to introducing a new ? operator but still strongly divided.

@seankhliao seankhliao added LanguageChange Suggested changes to the Go language error-handling Language & library change proposals that are about error handling. labels Jan 18, 2025
@ianlancetaylor
Copy link
Member

Thanks, but closing as a dup of #32845.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error-handling Language & library change proposals that are about error handling. LanguageChange Suggested changes to the Go language LanguageProposal Issues describing a requested change to the Go language specification. Proposal
Projects
None yet
Development

No branches or pull requests

7 participants