On Sat, Mar 13, 2021 at 2:14 PM Kevin Chadwick <m8il1i...@gmail.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.
> >
>
> This wouldn't solve the problem that I saw.


It really is just another way to say "the zero value of a map should be an
empty map". That is "it's initialized on the first write" was a suggestion
on how to implement those semantics, because to the programmer there would
no longer be a distinction between an empty map and a "nil" map. Currently,
the only difference is that you can't write to a nil map (as well as
comparing it to nil). That difference would disappear.

As an aside: It's the same for slices, currently. An empty slice and a nil
slice are identical in every way, except for comparison to nil.

AFAICT, it looks like I misread Ian's response as if make could always be
> used
> then it would solve the problem rather than being a cause of a compiler
> complication.
>

Compiler complication is not the concern. It would be easy to build any of
the suggested semantics into the language. It is just that none of the
suggestions so far seem clearly better - that is, they all come with their
own downsides.

The concern about the zero value consisting of 0 bytes is more about
performance. As long as that's the case, initialization is very close to
free. As soon as the zero value becomes some non-zero bytes, they have to
be written and that incurs a cost.

When we say "the zero value of a map should only contain zero bytes", what
we mean is that if I write
var m map[int]int
that should a) reserve some memory for m (currently that's as many bytes as
a pointer needs) and b) those bytes should be zero after that.
If "make was called implicitly" or anything like that, those bytes wouldn't
be zero anymore - they would contain a pointer to an allocated map header.

It's as simple as that. You might feel that preserving that property is not
important or less important than having a useful zero value for maps.
That's fine. But that's the property Ian is talking about.



>
> --
> 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/87d37b24-14ba-1d9c-35eb-eef9832a10bc%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/CAEkBMfHzsX2mxLu-ihqkR_btNgkzHsae1b%3DrqJhc-JXQousrDw%40mail.gmail.com.

Reply via email to