I think you should still wait for the outcome of that issue.

On Mon, Feb 26, 2024 at 10:39 AM brien colwell <xcolw...@gmail.com> wrote:

> I learned a lot from this thread, thank you.
>
> Intuitively the spec seems to conclude a pointer to an empty struct is a
> different type of pointer? Normally a pointer wouldn't be able to change
> values during execution, so we can do things like key maps by pointers.
>

Note that this is not quite right. It is allowed for addresses to change.
It is possible to implement Go with a moving GC, for example, by design.
Pointers in a map would have to be updated by the GC in that case.


> But if every evaluation of the empty struct pointer can lead to a
> different outcome, isn't that the same as the address of the pointer
> arbitrarily changing?
>

Not *quite*. For example, you should be able to rely on `a == a` to always
be true (the spec says "distinct variables", so two pointers to
non-distinct variables would still have to compare equal). So, if you just
pass around a pointer to a single-variable, that should always have the
same behavior: https://go.dev/play/p/5nmqwnCiq9L

But it means that if you have two distinct variables of zero size, you can
no longer meaningfully talk about whether they are "the same pointer". They
might be, or they might not be.

In a sense, it would be valid for `==` on pointers to zero-sized types to
always evaluate to `true`, but it wouldn't be valid for them to always
evaluate to `false`. There are cases where you can rely on two pointers
being the same, but you can *never* rely on them being *different*.

Genereally, though, I'd argue that it's safest to just not assume anything
about pointers to zero-sized variables.

Otherwise if two pointers are different, then the interface comparison of
> two pointers must be different also since the pointer address does not
> change?
>
>
> On Feb 25, 2024, at 1:02 AM, tapi...@gmail.com <tapir....@gmail.com>
> wrote:
>
> 
> The behavior of Go 1.9 or 1.10 is even more weird.
> They make the following code print false. ;D
>
> package main
>
> type T struct {}
>
> func main() {
>   var a, b = &T{}, &T{}
>   println(a == b || a != b)
> }
>
>
> On Sunday, February 25, 2024 at 4:30:22 PM UTC+8 tapi...@gmail.com wrote:
>
>> Absolutely a bug.
>>
>> On Thursday, February 22, 2024 at 6:55:49 PM UTC+8 Brien Colwell wrote:
>>
>>> I'm confused by this output. It appears that the interface of two
>>> different pointers to an empty struct are equal. In all other cases,
>>> interface equality seems to be the pointer equality. What's going on in the
>>> empty struct case?
>>>
>>> ```
>>> package main
>>>
>>> import "fmt"
>>>
>>> type Foo struct {
>>> }
>>>
>>> func (self *Foo) Hello() {
>>> }
>>>
>>> type FooWithValue struct {
>>> A int
>>> }
>>>
>>> func (self *FooWithValue) Hello() {
>>> }
>>>
>>> type Bar interface {
>>> Hello()
>>> }
>>>
>>> func main() {
>>> a := &Foo{}
>>> b := &Foo{}
>>> fmt.Printf("%t\n", *a == *b)
>>> fmt.Printf("%t\n", a == b)
>>> fmt.Printf("%t\n", Bar(a) == Bar(b))
>>>
>>> c := &FooWithValue{A: 1}
>>> d := &FooWithValue{A: 1}
>>> fmt.Printf("%t\n", *c == *d)
>>> fmt.Printf("%t\n", c == d)
>>> fmt.Printf("%t\n", Bar(c) == Bar(d))
>>> }
>>> ```
>>>
>>> Prints (emphasis added on the strange case):
>>>
>>> ```
>>> true
>>> false
>>> **true**
>>> true
>>> false
>>> false
>>> ```
>>>
>>>
>>> --
> 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/JBVqWYFdtC4/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/e4429119-5c91-44d1-93c4-dc877efdd7b9n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/e4429119-5c91-44d1-93c4-dc877efdd7b9n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> --
> 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/96D5EEE6-5E5B-4064-BAA3-AD8B985FE1F0%40gmail.com
> <https://groups.google.com/d/msgid/golang-nuts/96D5EEE6-5E5B-4064-BAA3-AD8B985FE1F0%40gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAEkBMfF6-KiCDG41NKk0NjKtbOpdUppiiHtiZk1xXT7Vg4urwQ%40mail.gmail.com.

Reply via email to