2

package main

// go is very frustrating. in their efforts to keep the language simple, they've broken its consistency :(

// A A is just some arbitrary interface
type A interface {
Foo()
}

// B is an interface requiring a function that returns an A
type B interface {
Bar() A
}

// Aimpl implements A
type Aimpl struct{}

// Foo is Aimpl's implementation of A
func (a Aimpl) Foo() {}

// Bimpl attempts to implement B
type Bimpl struct{}

// Bar is Bimpl's attempt at implementing B.
// problem is, if Bar returns an Aimpl instead of A, the interface is not satisfied
// this is because Go doesn't support implicit upcasting on returns from interfaced objects.
// if we were to simply change the declared return type of Bar() to 'A', without changing
// the returned value, Bimpl will satisfy B.
func (b Bimpl) Bar() Aimpl {
return Aimpl{}
}

var _ B = Bimpl{}

func main() {

}

Comments
  • 1
    Go is not really an object oriented language. It also takes "composition over inheritance" to the max.

    It can work fine, if you write small modules with a very flat type architecture, and ditch the OOP mindset as much as possible.

    The type system is kind of garbage, compared to Rust for example. To me it feels like an awkward mix of JS, Python and C... but it's so freaking fast and easy to write a simple concurrent backend in Go.

    I'm not in love with her, but Node looks really gross since I met Go.
  • 1
    You mentioned Rust, oh it makes me so happy!
Add Comment