-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: add sugar multiple keys to map to be tuples #63431
Comments
Thought: |
This does seem like some interesting syntax sugar that might help in some specific situations, but personally I think we should wait to see how #63221 fares first, since this seems like essentially a further layer of sugar on top of that. If that other more general proposal were not accepted, I think it would be hard to justify accepting this one that is effectively a less orthogonal version of the same idea (limited only to maps). Even if that other proposal is accepted, I would still suggest waiting until it has been implemented and available for some time to see how often situations like Any "syntactic sugar" that hides something has the cost of adding another thing that a new Go developer might find in an existing codebase and not know its name to learn about it. I think that can be justified if a particular pattern appears often enough that a new developer is likely to encounter it often and probably have access to a colleague who can help explain what it means, but for less-frequently-used constructs it's more likely that someone could go several years without encountering it, and then encounter it for the first time when the original author is not around to explain it, leaving them stuck. Of course that's a tradeoff, not a hard rule. But it's a tradeoff that's easier to make with the experience of seeing how the non-sugared version arises in practice, and it's not really possible to do that when it's dependent on another second level of syntax sugar that isn't available yet either. With that said, I don't really have a strong opinion on the proposal itself, only on the "meta" that it seems a little early to get good data on whether it would be useful and thus make a decision about it. |
While this is an interesting idea, it is a rather narrow use of tuples. If Go were to accept this syntax, it would be more consistent to also allow tuples elsewhere (and this may be required in order to work with It is trivial to define a local struct type where required. Therefore, marking this as likely decline. Leaving open for four weeks for additional comments. |
There were no further comments, and no change in consensus. |
Author background
Related proposals
is:issue label:proposal maps in:title
search suggest to me noIf so, how does this proposal differ?If so, how does this differ from previous error handling proposals?If so, how does this relate to the accepted design and other generics proposals?This is based with concepts of #63221 (but could be refactored to not be).
In case #63221 is refused then I think this is especially valuable.
Proposal
Allow to define multiple types in the keys of maps:
And have it be equivalent to creating a tuple of the same types:
Indexing into the map would be sugar for
pack
:Would be:
Lastly maps literals would get the same treatment:
Into:
Current alternatives are:
Which works but is verbose.
m[a][b]
but is very awkward to deal with when considering keys that can be missing, awkward collection when deleting elements and adding elements which need to check if the inner map exists. This gets very bad to use fast when using lots of elements in the key. And performance is in almost all cases worst. *This fits unique edge cases you need to iterate over an inner map, or pass the inner map around.KeyType = Type .
→KeyType TypeList .
TypeList is turned into an anonymous struct using proposal: spec: tuples as sugar for structs #63221struct
tuple.KeyList = Key { "," Key } .
KeyedElement = [ Key ":" ] Element .
→KeyedElement = [ KeyList ":" ] Element .
, specifying more than one key is invalid except for map where it is then packed into one value using proposal: spec: tuples as sugar for structs #63221pack
rules.Index = "[" Expression [ "," ] "]" .
→Index = "[" ExpressionList [ "," ] "]" .
, specifying more than one key is invalid except for map where it is then packed into one value using proposal: spec: tuples as sugar for structs #63221pack
rules.Ok so imagine you just explained and demonstrated how maps exists.
Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit.Show example code before and after the change.
BeforeAfterIf so, what quantifiable improvement should we expect?How would we measure it?Costs
map[string, float64, int]complex128
andm["a", 42, 1337]
are self explanatory however in the rare cases someone would try to integrate this with iteration andencoding/json
(throughreflect
) this would yield surprising results, at least if theses aren't updated to have special behavior for tuples.go/parser
would be enough.F0
,F1
, ...Fn
names) on map keys and map indexes with multiple entries.The text was updated successfully, but these errors were encountered: