-
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: add some more 'smarts' to literals of types using empty struct{} #23915
Comments
In case 1, if you have several differently named empty types, their zero values is identical to their initialised value, so you can initialise them, or not initialise them and the results is the same. Case 2 and 3. Remember that go optimises for the reader by reducing the number of different ways to represent the same syntax. I'd suggest either
or
as an unbuffered channel of an int and an unbuffered channel of a struct{} consume the same amount of space. |
These cases are in my mind not taking full advantage of Go.
|
so;
|
where is the int's value held then? |
i think of sets as something you cant have duplicates in, because that is what you are representing. |
Neither, var c OpaqueColor |
the empty Color type is just to save me typing, assume it wouldn't always be empty. this is about literals. |
There is no value (ha!) in explicitly initialising a field with an underlying type of |
cases 2 and 3 would be handled by #12854 which is more general |
reply confuses me; assume in all previous examples Color has a non-default value, say Color{1,2,3} or something
|
very close, it think it would leave the colon and empty braces in 2, but sure near enough. |
@splace you don't need to initialise fields with an underlying type of |
@davecheney which is what i'm looking for, example? |
@splace see your example here #23915 (comment) |
@davecheney that is an example showing whats bad? where you DO need to initialise fields with an underlying type of struct{} |
I don't. They don't need it. |
I think we're talking past each other. Your problem appears to be when there is more than one named field in a struct, then you need to explicitly name the fields when using a compact initialiser. I think this is unrelated to empty structs. |
my examples are all with unnamed fields!
it is completely and ONLY related to having to type out empty structs when there is enough info to infer them. |
I respectfully disagree, as I showed in my previous example. Your example's field has a name, it's name is
|
sorry, i thought these were called anonymous fields. (edit) |
but the point is being lost; as mentioned before #12854 is the same thing, only more general, applying as it does to other than just empty struct fields. personally i find the empty structs most significant conceptually and visually and would probably favour being explicit, over implicit, for other than them |
Looks to me like a more specialized version of #19642 |
For the container configuration volume set case using a map causes a compiler error if there are duplicate items. My mistake, a slice would not be better here. There is already a shortcut though:
Using a type for map[string]struct{} is another option although that loses the compiler error for duplicates: https://play.golang.org/p/kkB7bnwkhCe |
I'm not sure if this doesn't have some nasty drawback, but I have thought about it several times and would like to share it for your consideration.
Basically its about being able to miss out typing 'struct{}' in literals where its unambiguous.
(i did try making a value and naming it, say 'Nothing := struct{}{} ', but that’s still not great.)
Case 1
a few times, i have composed with types that have no state themselves, say; they implement some default static behaviour to meet an interface.
currently, when made literally, you need to specify the empty struct, which seems a bit redundant/unclear, or use named parameters which often results in stuttering.
clearer?
imagine if you had several empty types.
case 2.
when you use a map with empty structs values, say to make a set, the literal can get a bit bloated;
(code below cut/paste from a web search.)
clearer?
also, if you change to/from a slice of strings its trivial to update the literal.
case 3
using channels of empty struct{}
i think this is neater, since empty struct types are all the same anyway
The text was updated successfully, but these errors were encountered: