4

I have the following method defined:

func (o *MyObj) parse(something string) string {
    // Do stuff
}

This code (1) compiles just fine:

(&MyObj{}).parse(myString)

This code (2) compiles too:

m := MyObj{}
m.parse(myString)

But this code (3) doesn't compile:

MyObj{}.parse(myString)

With the error

Cannot call a pointer method on 'MyObj{}'

My questions:

  1. Why (2) compiles? I read that T method set doesn't include *T method set, hence I'd expect it not to compile.

  2. Given (2) compiles, why wouldn't (3) compile??

1
  • Are you asking about the rationale of Go's designers or for an authoritative source that explains the behaviour you observe? Commented Jul 20, 2021 at 10:28

1 Answer 1

4

The code in (2) m.parse(myString) compiles because calling the method on it equals to addressing and then calling the method on the pointer, i.e. (&m).parse(myString):

If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m()

However this shorthand is not available in (3) MyObj{}.parse(myString) because the literal MyObj{} is not addressable. From the specs: "Address operators":

The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; [...or other cases that don't apply].

And MyObj{} being a composite literal is neither of those; whereas m from example (2) is a variable.

Finally the same section about address operators specifies an exception that explains why the example in (1) (&MyObj{}).parse(myString) compiles:

As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal.

In this case you can take its address, parenthesize it and call the method, which is syntactically similar to the compiler shorthand for (2).

Sign up to request clarification or add additional context in comments.

1 Comment

The difficulty stems from the rather confusing terminology used by the language spec. MyObj{} is not strictly speaking addressable but, because it's a composite literal, taking its address is legal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.