Go2: Please Remove Type Method Expressions

Pretty Please?

Nick Santos
2 min readJul 31, 2018

A few folks in my Twitter feed are talking about what breaking changes they want in Go2, or Go++, or (my favorite) Go - -.

But there’s one “feature” in Go that I see new developers get baffled over all the time.

I wish we’d remove it.

// https://play.golang.org/p/QRjeevkktd0package mainimport (
"fmt"
)
type Point struct {
x int
y int
}
func (p Point) X() int {
return p.x
}
func main() {
fmt.Println("Point.X", Point.X())
}

When you compile this code, the Go compiler complains:

prog.go:17:32: not enough arguments in call to method expression Point.X
have ()
want (Point)

So the first thing that the beginner developer will do is ask “Wait, I didn’t define any method that takes Point as an argument. Where did this method come from?”

They change main to:

// https://play.golang.org/p/GSkIvlYIc79
// ...
func main() {
fmt.Println("Point", Point)
}

And they get a new compiler error:

prog.go:17:13: type Point is not an expression

Oh, god, now they’re even more confused. Point isn’t an expression but Point.X is? How does that even make sense? I thought . meant “get a property of an expression?” So what is Point? How did I mess this up?

Experienced developers grasp the difference between the instance method and the type method easily. Developers learning Go do not.

The language fix is simple: don’t overload the . operator for this feature. Create a new operator. So Point.X would be invalid, but Point#X would refer to the type method that takes one argument, an instance of the struct.

Thanks for reading! If you know what the process is for making this a formal proposal for Go2, I’d love to chat!

--

--

Nick Santos
Nick Santos

Written by Nick Santos

Software Engineer. Trying new things @tilt_dev. Formerly @Medium, @Google. Yay Brooklyn.

Responses (1)