-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Go 2: make(...) should have the last (optional) parameter for configuring default values for all types (slices, maps and chans) #65332
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
how does this work for maps, as the title explicitly mentions it? what does filling for a map even mean? |
if mp is a map |
this doesn't appear to be true, as now you can't know what |
This doesn't make any sense to me, we have the |
would slightly disagree here, i get your point though rn, i need to do the following (or its equivalent)
instead
now if you do want to check the what the default value is in a call hierarchy, you'll need to go to initialization as you said for channels I can't do this that simply anyway, if I want to signal a non-zeroed default value from closed channels |
This feels to me like multiple non-trivial proposals in one, although I acknowledge that some of them depend on others:
Items 1 and 2 seem like far more significant additions to the language -- they affect behavior of those types globally, rather than just at one callsite -- and so I think this proposal is kinda burying the lede by focusing primarily on the syntax extension in point 3. I'd suggest that it's perhaps better to start by proposing the new map and channel behaviors, describing how they would behave under all of the different operations that maps and channels support. The syntax for constructing maps and channels with those new features enabled is of course still an important part of the proposal, but I don't think it's the most important part. (The behavior for slices seems a little tricky too, despite the ability to pre-populate those. Should a slice also "remember" its default value, in a similar way as for maps and channels, so that an out-of-bounds access can return that default value? Should excess capacity added by |
it is non-trivial for sure.
the way I see it - configuring the default via an optional parameter in make, is not a behavior change (as in replacement). there is no changing of the old behavior (old code imho will remain a 100% backwards compatible)
for slices append will need to take the default value into account, oob should remain as it is, aligning with the current behavior |
What I meant in my previous comment is that the change to the behaviors of maps or channels has consequences beyond just the addition of a new parameter to
The above are just some examples of what I mean about this being a more "global" idea, rather than just a change to the When I suggest proposing the default values idea as a whole, I mean a proposal that addresses the implications more broadly, showing that the new feature can fit in to all parts of the existing language. |
@rishabhkhemka it would be very helpful to see some real world examples that might benefit from this feature. Examples where returning non-zero default values would actually result in better code / design. |
Go Programming Experience
Experienced
Other Languages Experience
C++, Python
Related Idea
Has this idea, or one like it, been proposed before?
No, as per preliminary (not extensive) research.
Does this affect error handling?
No
Is this about generics?
No
Proposal
make(...) should have the last parameter for configuring default values for all types (slices, maps and chans)
the behaviour can be simulated by wrapping the types with custom methods but is not ideal since we don't have operator overloading.
adding wrappers for configuring default behaviour goes against the readability aspect of the language.
Language Spec Changes
No response
Informal Change
make([]int, 2, 10, 1) // -> should fill the slice / underlying array with 1s
similarly for maps(default values) and chans(values for closed chans)
there might be an alternate way to introduce this via special constructors in the experimental packages.
I believe make is the most appropriate way for configuring the default behaviour.
I am fine with either.
Is this change backward compatible?
yes, make is already variadic
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
this doesn't add any cognitive overhead, nor does it remove any.
it does remove a lot of boilerplate code when there is need for configuring default behaviour of inbuilt data structures.
Cost Description
No response
Changes to Go ToolChain
No response
Performance Costs
No response
Prototype
No response
The text was updated successfully, but these errors were encountered: