Dereferencing interface pointers

3,428 views
Skip to first unread message

David Gray

unread,
Jan 16, 2011, 9:19:52 AM1/16/11
to golang-nuts
Today I updated my installation of Go on OS X. My old version was 6006
and my new version is 7105.

The following code compiles with version 6006 but not with 7105.

----------------------------------------------------------------
package xxx

import (
"os"
)


type stream interface {
ReadByte() (os.Error)
}


func T(s *stream) {
s.ReadByte()
}

type INtEncoding struct {
source *stream
}


func (enc *INtEncoding) ReadByteOrEOS() os.Error {
enc.source.ReadByte()
return nil
}

----------------------------------------------------------------

To get this code to compile with 7105 I need to dereference pointers
to the stream interface, i.e., the following code compiles with both
6006 & 7105.

----------------------------------------------------------------
package xxx

import (
"os"
)


type stream interface {
ReadByte() (os.Error)
}


func T(s *stream) {
(*s).ReadByte()
}

type INtEncoding struct {
source *stream
}


func (enc *INtEncoding) ReadByteOrEOS() os.Error {
(*enc.source).ReadByte()
return nil
}

----------------------------------------------------------------

Has the language definition changed or is this a compiler problem?

Regards,
David

Jessta

unread,
Jan 16, 2011, 9:52:46 AM1/16/11
to David Gray, golang-nuts
On Mon, Jan 17, 2011 at 1:19 AM, David Gray <da...@formal.ie> wrote:
> Has the language definition changed or is this  a compiler problem?

Why are you using a pointer to an interface?
This is necessary unless you're going some kind of unsafe magic.

There was a change that no longer auto deferences pointers to
interfaces, since this is almost always a bug.

"The language change is that uses of pointers to interface values no longer
automatically dereference the pointer. A pointer to an interface value is more
often a beginner’s bug than correct code."

http://golang.org/doc/devel/release.html#2010-10-13

- jessta
--
=====================
http://jessta.id.au

David Gray

unread,
Jan 16, 2011, 12:08:50 PM1/16/11
to golang-nuts
Hi Jessta,

Thanks for the response.

This has solved my compilation problems with the new version of the
compiler, but I should note that my software with pointers to
interfaces has been working quite happily for months.

Regards,
David

Steven

unread,
Jan 16, 2011, 12:37:35 PM1/16/11
to David Gray, golang-nuts
Yes, but it was incorrect, because it uses redundant indirection. Just don't use pointers to interfaces, they usually aren't necessary, unless you can specifically think of reason that makes you case different from the general (99.99% of the time) case. They certainly aren't in the example you gave. If you just stored a stream instead of a *stream in your struct, it would have 0 impact on your program, except for saving an indirection. Chances are, you could use the struct without a pointer (and have the method be a value, not pointer, method) and it would still work, since you're probably storing a pointer in interface itself, and that's the only one that matters.

Usually when people use pointers to interfaces, it's because they misunderstand how interfaces work, and assume they work with pointer conversion as in C++  (which they don't).

Russ Cox

unread,
Jan 18, 2011, 9:37:55 AM1/18/11
to David Gray, golang-nuts
> This has solved my compilation problems with the new version of the
> compiler, but I should note that my software with pointers to
> interfaces has been working quite happily for months.

Yes, but only because you hadn't updated since last August.

Russ


changeset: 6437:54014073d377
user: Russ Cox <r...@golang.org>
date: Thu Sep 30 14:59:41 2010 -0400
description:
gc, spec, tests: no auto-indirect of pointer to interface value

Implies no embedding of pointer to interface value either.

R=gri, iant, ken2, r, r2
CC=golang-dev
http://codereview.appspot.com/2289041

Reply all
Reply to author
Forward
0 new messages