On Sat, Mar 13, 2021 at 1:24 AM Matthew Holiday <matthew.holi...@nytimes.com> wrote:
> I don't think we should change creation; what about having the first > insert make the map if it's nil? > > It seems that would be fairly transparent. > I assume you accidentally hit "Reply" instead of "Reply All". This isn't as simple either, unfortunately: func F(m map[int]int) { m[42] = 23 } func main() { var m map[int]int F(m) fmt.Println(m[42]) } F might create the map, but the change has to be propagated back somehow - and all it got is a nil-pointer. No offense, but again, if it was that simple, it would have already happened :) No one is arguing that having to explicitly initialize a map (the same, BTW, goes for channels) is not awkward. But any solution comes with its own set of problems, be it having to write `*map` everywhere, introducing call-by-reference semantics, but only for some types, not for others or significant performance penalties. It's been over a decade and we didn't find a panacea yet - it's becoming increasingly unlikely that we find one. And if we where to go through the churn of orchestrating a deep language change like this, breaking almost every Go program out there, it really has to be worth it and not just trade one wart for another. > > On Fri, Mar 12, 2021 at 5:03 PM 'Axel Wagner' via golang-nuts < > golang-nuts@googlegroups.com> wrote: > >> >> >> On Sat, Mar 13, 2021 at 12:01 AM Kevin Chadwick <m8il1i...@gmail.com> >> wrote: >> >>> I do not need to understand the difficulties that exist, though I >>> struggle to understand the zero bytes property as surely even an empty >>> string has a 4 or 8 byte pointer. >>> >> >> A string is effectively a >> struct{ >> data *byte >> len int >> } >> If you zero that, you get a nil pointer and a 0 length, which is a valid >> representation of an empty string. >> >> The same is true for every Go type. A nil pointer is all zero bytes (so >> pointing at address 0, which is not accessible), maps/funcs/chans are >> de-facto pointers (we often say "they are pointer-shaped"), so their zero >> value is also just a pointer to address 0. A nil slice is a >> struct { >> data *T >> len int >> cap int >> } >> which, if zeroed, points at 0 with length/capacity 0. And so on. >> >> The zero value of any Go type is just whatever it is represented as in >> memory, with all bytes set to 0. >> >> My original thinking was that either the function call or initialisation >>> could run make under the covers. >> >> From Ian in those threads copied above "While not requiring the make >>> call". >>> >>> Why not require make by calling it, behind the scenes? >>> >> >> But that is exactly the loss of "the zero value is all 0 bytes" we are >> talking about. It would mean if you write `var m map[int]int` the compiler >> needs to create instructions to initialize `m` to something that is not >> zero bytes (a.k.a. "call make behind the scenes"). And if you do `m := >> make([]map[int]int, 1e6)` it needs to run those instructions a million >> times - instead of just getting a block of 0-bytes from the runtime. >> >> Of course it's *possible* to abandon this property. Lots of languages >> (dare I say most languages) don't have it. We'd just prefer not to. >> >> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "golang-nuts" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to golang-nuts+unsubscr...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/golang-nuts/AAB589F5-DA04-4468-A745-77F9177EAF76%40gmail.com >>> . >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGUjnqSnBmKZ%3DH6UHWmK%3DSFTigNLnYiBi%3DcT2Km2UzskQ%40mail.gmail.com >> <https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGUjnqSnBmKZ%3DH6UHWmK%3DSFTigNLnYiBi%3DcT2Km2UzskQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > *Matt Holiday* > Senior Gopher, Marketing Technologies > > 620 Eighth Avenue > > New York, NY 10018 > > matthew.holi...@nytimes.com > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEW2aS7cnUBnPWXqwVu5%3DGp9CqBjTapDRizzeOJ%2Bg2Y1Q%40mail.gmail.com.