-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: Go 2: empty
as an alias for struct{}
#57825
Comments
I suspect that |
This comment was marked as spam.
This comment was marked as spam.
empty
as an alias for struct{}
Well sure, but
Anyway it would just be nice. It took me a little while to discover |
The conventional name for this type in other languages is |
And I expect that the usage will go down as more generic implementation of things, such as sets, are created and added to various places. The most common direct usage will probably be in |
The frequency-of-use argument is orthogonal. I have more |
I don't think the frequency argument is unimportant. We could introduce all kinds of predeclared names. Each one is a tax on everyone in the Go community, because they have to learn it and remember what it means. Therefore, the bias should always be against adding a new name. They should only be added when they bring a significant benefit. An example of such a benefit would be that it makes a lot of existing code easier to read. |
Nicely put. These are all good points:
I don't think the answer to these has anything to do with how much another language construct is used. An empty struct declaration is an unexpected way to declare an empty value, which in my mind has nothing to do with structs. Enough said, it was fun to think about this morning... |
This is actually a non-issue here. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Some rough counts on my code code, Go stdlib, and contents of my module cache:
It seems Is that enough to warrant adding a new predeclared identifier? I don't know. But I did have to explain what this odd "struct{}{}" thing means to a few junior devs over the years; it's kind of non-obvious, and IMHO rather ugly. On the other hand, you'd also have to explain what |
If something like this is considered, a corollary that I'm not entirely sure about myself could be to allow a type name to be used directly as its own zero value. That would mean that |
I believe in most [edit]: maybe |
I think the opposite. The fact that it has no size is a minor optimization detail. I think most people are more interested in the fact that it contains no useful information. |
Maybe // var m map[int]_
m := map[int]_{}
m[0] = _ is a better choice by this logic? |
It's easy to write The existing name Also the emoji voting is not in favor. Therefore, this is a likely decline. Leaving open for four weeks for final comments. |
The hope was to drive it to a standard, and not have Yes "any" is different, although... Likely decline due to emoji voting makes sense. |
No change in consensus. |
Very simple proposal here.
Instead of:
how about
I love the change from
interface{}
toany
, making code, stacks, intellisense, etc., easier to read.I dislike the
struct{}{}
to represent the concept of an empty value.I propose
empty
because it reduces curly brace clutter, and makes it obvious that the value is nothing.The text was updated successfully, but these errors were encountered: