Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A similarity to your struct of fileops, would be Go to allow implementing an interface will nil methods. Like:

  func (f *file) Rename() = nil
Then later test for nilness with: if f.Rename == nil, or with better readability:

  if v, ok := f.(interface {Rename()}); ok && v == nil // -> if true, then nil method
We could define a Type Assertion to return "nil, ok" when all methods of the interface it is being passed to are implemented nil methods.

  type NotOftenImplemented interface {
    RareMethod()
    SuperRareMethod()
  }

  if v, ok := data.(NotOftenImplemented); ok && v == nil {
    // RareMethod() and SuperRareMethod() are nil
  }

  switch data.(type) {
    case NotOftenImplemented: // would be supposed to match ONLY if AT LEAST >= 1 of the methods of this interface is non-nil
  }
Of course it would create a phenomenon where all callers of any interface methods would now fear of calling any method without first testing for its nillness.

On another hand, there is also sometimes methods implemented with a single-line of panic("Not Yet Implemented") so...



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: