News for hackers. Stuff that matters.
Re:Why I like Golang's interfaces by majewsky on Thursday April 13, 2017 @01:23PM (#14109024)
I'm not convinced. Suppose that a struct type Foo implements an interface Fooer, and I want to do something like this:

    func workOnInterfaceList(foos []Fooer) {...}

    func workOnStructList(foos []Foo) {
        workOnInterfaceList(foos) //compile error: cannot cast []Foo into []Fooer
    }
It's obvious how to resolve this:

    func workOnStructList(foos []Foo) {
        foos2 := make([]Fooer, len(foos)
        for idx, foo := range foos {
            foos2[idx] = foo //casts Foo into Fooer implicitly
        }
        workOnInterfaceList(foos2) //ok now
    }
Why can the compiler not insert this automatically (esp. given that [] is a builtin type)?

But then again, Go has a reputation of making you write the same code over and over again, so I'm not surprised. (And I say that as an empathic supporter of Go.)

As an aside, if you think this example is artifical, here's it happening in real-world code: https://github.com/majewsky/sqlproxy/blob/f5b297e7dce14c0453...

Re:Why I like Golang's interfaces by echlebek on Friday April 14, 2017 @02:45PM (#14116274)
Your code doesn't contain the definition of Foo and Fooer but I'll assume Foo is a concrete type and Fooer is an interface that Foo satisfies.

The reason that []Foo is not the same as []Fooer is simple: they are different data types. They have a different layout in memory.

Internally, an interface value is a pair of pointers. One points to the concrete value, and the other points to the concrete type of the value.

The compiler needs to know the size of the things it's working with in the case of slices, because it needs to know how to compute offsets for indexing and iteration. It could make an implicit copy at runtime, but that could trigger a costly operation that would be invisible to the user. Go wants expensive operations to be visible, so you have to make the copy yourself.

Re:Why I like Golang's interfaces by zellyn on Thursday April 13, 2017 @01:46PM (#14109240)
I'm not convinced either. It's not clear that the compiler should do that. You just allocated a new array, and (may have) made copies of the values in the original array. Reordering the new array will not reorder the original one. etc. etc. It's not clear to me what you'd want to do in every case, let alone to a compiler.

Just curious: did you mean empathic or empathetic or emphatic? :-)

Re:Why I like Golang's interfaces by majewsky on Friday April 14, 2017 @03:48PM (#14116719)
Oh dear, of course I meant "emphatic". I'm certainly not a Betazoid. :)