Can you explain what you mean by tokens? Do you mean something like:

```
var red, green, blue = new(struct{}), new(struct{}), new(struct{})
```

If so, I don't see how that's useful. Why not use integers or bytes instead?

On Wed, May 4, 2022 at 12:35 AM 'Dan Kortschak' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> On Wed, 2022-05-04 at 07:58 +0200, 'Axel Wagner' via golang-nuts wrote:
> > > As a tangent, I don't understand why this wasn't made unambiguous
> > > in the language spec. Why not have `new(struct{})` always allocate
> > > a new pointer? Who's allocating all these empty structs on the heap
> > > where this is something that needs to be optimized for? Is that
> > > really worth complicating the language? 🤔
> > >
> >
> > I don't know why that decision was made. I do believe there are some
> > less obvious cases, where you at least have to add special casing in
> > the implementation (e.g. make([]T, x) would have to check at runtime
> > if x is 0). But I agree that it would probably be okay to change the
> > spec here.
>
> For zero-sized types (as opposed to zero-length types like zero-length
> []T), there is a good reason not to allocate a new zero-sized slot on
> the heap; since zero-sized values are commonly used as tokens, the
> language allows them and they don't store anything, it is fruitless
> work to go through the motions of asking the allocator to get a pointer
> when the same one can be used always with close to zero cost (does
> sizeof equal zero).
>
> For history, the treatment of this was introduced here
> https://codereview.appspot.com/10136043 (in malloc.goc).
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/b-WtVh3H_oY/unsubscribe.
> To unsubscribe from this group and all its topics, 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/e3cff142d482ad17973538256a6ded7923d9a2e5.camel%40kortschak.io
> .
>

-- 
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/CAKbcuKiNFqs%3DhmHoX-Q2Ng2nLVaxGgbXpcgXn3bvXef0sRJRZg%40mail.gmail.com.

Reply via email to