On Tue, Mar 10, 2020 at 10:08 PM robert engels <
ren...@ix.netcom.com> wrote:
>
> I think what the OP was trying to say, is that with ‘const’ the compiler could safely use a pointer receiver rather than a copy, and also enforce that const are not modified, and only passed as const args (may require a heap allocation though, so maybe only if struct is of a certain size based on platform/compiler opts).
>
> I think he has a point.
>
> When you see a ‘pointer receiver’ a developer probably assumes the code needs to modify the structs - thus you must pass a pointer. But if you are only declaring a pointer to avoid an expensive large structure copy, you are giving misleading information - and possibly causing bugs due to incorrect later code modifications.
Thanks. I don't see how to make that work, though. Consider
type S struct { a [10000]byte }
func (s const S) PrintLater() {
go func() {
time.Sleep(time.Second)
fmt.Printf("%s\n", s.a)
}()
}
func F() {
var s S
s.a[0] = 'a'
s.PrintLater()
s.a[0] = 'b'
}
With a value receiver the behavior is straightforward. With a pointer
receiver this is a race condition. With a const receiver, what is it?
It shouldn't be a race; after all, we've promised that the const
receiver won't modify the receiver. So we have to copy the struct.
Ian