Skip to content

proposal: Go 2: noncompare keyword to make structs non comparable #67196

Closed as not planned
@Chig-Beef

Description

@Chig-Beef

Go Programming Experience

Intermediate

Other Languages Experience

Python, C#, JS,

Related Idea

  • Has this idea, or one like it, been proposed before?
  • Does this affect error handling?
  • Is this about generics?
  • Is this change backward compatible? Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit

Has this idea, or one like it, been proposed before?

No (not that I know of)

Does this affect error handling?

No, if implemented correctly it should be identical

Is this about generics?

No

Proposal

Going through the codebase for ebitengine I noticed this code.

type ColorM struct {
	impl affine.ColorM

	_ [0]func() // Marks as non-comparable.
}

I thought this was strange, but understood what was happening. This issue here is that I don't think everybody would be able to understand what's happening especially without that comment. The fact is that this is the easiest way to do this that I know of, and it doesn't look great. It also doesn't seem very Go-like in my opinion, as it is very unreadable (without prior-knowledge or comment) and most Go code is relatively easy to understand.

The proposal is to create a way to simplify marking structs as non-comparable. I personally don't know what this should look like, but it could be something like this.

type Example noncompare struct {

}

Switch out the keyword for a nicer one (just an example). This is only one suggestion though, but I don't think I'm the person to ask for the most Go-like solution.

Language Spec Changes

A new keyword would be added to mark structs as non-comparable (or an alternative solution).

Informal Change

It is obtuse to mark structs as non-comparable, and therefore there should be a simpler way (whether with a keyword or not) to mark them as such.

Is this change backward compatible?

Maybe, depending on how it is implemented. With a keyword as suggested, this wouldn't be backwards compatible, however, there are definitely solutions that are.

Orthogonality: How does this change interact or overlap with existing features?

If this change occurs it would make it easier (especially for new programmers of Go) to read the code and understand it. It would also get rid of unnecessary comments. I wouldn't know how to measure that.

Would this change make Go easier or harder to learn, and why?

Very much easier.

type ColorM struct {
	impl affine.ColorM

	_ [0]func() // Marks as non-comparable.
}

In the above code you either have to treat the line as a black box that just magically marks structs as non-comparable, or learn what each individual part means.
A keyword would be explainable to its full extent with one sentence.

Cost Description

My worry is that we're adding another keyword to the language. To get around that you could scope the search for that keyword only in that one context, so that it doesn't break backwards compatibility, however, that may make it a more complex issue.

Changes to Go ToolChain

gofmt and gopls would definitely be affected, but other than that I don't think much more.

Performance Costs

In regards to compiler performance, I wouldn't expect it to be too expensive, as it's one extra token (depending on implementation). Runtime cost should be nothing (if not faster).

Prototype

type Example1 noncompare struct {

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions