Hacker News new | ask | show | jobs
by NateDad 3563 days ago
Not quite sure why this isn't just

  type foo int:
      func MethodA():
          pass

Analagous to this:

  type foo int
  func (foo) MethodA(){}
Why opening and all that? Or is that a way to add methods to types outside the current package? If so, you're opening up a whole new can of worms.
1 comments

Your example looks good as well. Opening gives more flexibility with how the code can be structured, say that you have two interfaces:

  type I interface {
    A()
  }

  type J interface {
    B()
  }
And you have two structs that implement them, you can then group methods from the same interface together:

  type X struct{}
  type Y struct{}

  func (x X) A() {}
  func (y Y) A() {}

  func (x X) B() {}
  func (y Y) B() {}
I've seen that used in the Golang codebase, and sometimes it does seem useful.