News for hackers. Stuff that matters.
Re:Things about programming I learned with Go by suchabag on Tuesday August 22, 2017 @06:18AM (#15071034)
> Authors of Go wanted to give users more flexibility by allowing them to add their logic to any structs they like. Even to the ones they’re not authors of (like some external libraries).

Except that you can't. "You "cannot define new methods on non-local type[s],". You have to compose them. That makes the struct function definition syntax a bit moot in my opinion.

Re:Things about programming I learned with Go by majewsky on Tuesday August 22, 2017 @06:26AM (#15071061)
I've never thought of that syntax as conveying a particular statement. They could also have done

  func *Receiver.method(arg int)
instead of

  func (r *Receiver) method(arg int)
The advantage is that you get to name the receiver instead of having to use a keyword name like `this` or `self`. I've come to like this design decision.
Re:Things about programming I learned with Go by icebraining on Tuesday August 22, 2017 @09:37AM (#15072217)
The advantage is that you get to name the receiver instead of having to use a keyword name like `this` or `self`. I've come to like this design decision.

self, at least if you're talking about Python, is not a keyword, it's just a convention. You can use whatever your want.

Re:Things about programming I learned with Go by leshow on Tuesday August 22, 2017 @03:27PM (#15075602)
It's not just python that uses self. Rust, for example, it's sugar for self: Self
Re:Things about programming I learned with Go by leaveyou on Tuesday August 22, 2017 @07:14AM (#15071289)
He speaks about non-local receivers.

You can't have:

func (r *otherPackage.Receiver) method(arg int)

Re:Things about programming I learned with Go by literallycancer on Tuesday August 22, 2017 @08:56AM (#15071870)
What's the issue with composing a new type that has otherPackage.Receiver in it, and defining the new method on the new type?
Re:Things about programming I learned with Go by leaveyou on Tuesday August 22, 2017 @09:08AM (#15071963)
For me there is no issue.
Re:Things about programming I learned with Go by ptman on Wednesday August 23, 2017 @08:15AM (#15080815)
I'm guessing: you still can't access unexported members (private vs Public).
Re:Things about programming I learned with Go by junke on Tuesday August 22, 2017 @03:28PM (#15075611)
The question could be asked the opposite: why is the new type necessary?
Re:Things about programming I learned with Go by majewsky on Tuesday August 22, 2017 @05:21PM (#15076506)
Because if you allow non-local method declarations, you have a ton of stuff to think about:

- Are these methods exported?

- If yes, how do you import them? Explicitly or implicitly?

- Given a method call, how can the developer tell where it was defined? How can she know if the same method call is available when using the same library in some other project? Remember that not everyone uses IDEs.

- etc.

I can see why the Go designers decided that it's just not worth it. I've seen how you turn a language into a mess with non-local method declarations (cough Ruby cough).

Re:Things about programming I learned with Go by mjkpl on Thursday August 24, 2017 @01:54PM (#15092393)
Yes, you're right. I was somehow convinced that this is possible. Which is BTW not a good idea anyway (or at least I really don't like similar concept in Ruby - monkey patching).

Do you know why Go authors decided to put method definitions outside types definitions? Technically I don't see anything to stop the syntax from supporting the otherwise.