News for hackers. Stuff that matters.
Re:Why I like Golang's interfaces by realharo on Thursday April 13, 2017 @10:58AM (#14107481)
I've been writing quite a bit of TypeScript recently (which works similar to Go in this regard), and I must say I've become quite a fan of structural typing.

Especially when you have features like union and intersection types (which Go doesn't have), you can really mold the structure into whatever you need (i.e. to fit whatever data model you're working with), and be very productive without having to come up with weird inheritance hierarchies "just because".

It'd be nice to have the option to explicitly enforce nominal typing in specific places (maybe with its own keyword), but for most cases, structural typing seems good enough so far.

Re:Why I like Golang's interfaces by infogulch on Thursday April 13, 2017 @12:04PM (#14108169)
I admit the syntax is less than ideal (especially with gofmt), but I believe adding two interfaces to an interface definition is equivalent to union:

    func myrw(rw interface { io.Reader; io.Writer }) {
        rw.Read(nil)
        rw.Write(nil)
    }
https://play.golang.org/p/D-lJ1pL3MZ
Re:Why I like Golang's interfaces by camus2 on Thursday April 13, 2017 @07:47PM (#14111714)
That's not a union, that's a product of 2 types. A union is one or the other but not both. You rw here is both a reader and a writer, you can't pass something that is either a reader or a writer, which makes your snippet useless in use-cases where unions are appropriate.

Of course, Go compiler itself uses "unions" with runtime-tricks to implement them. But we're talking about runtime then, not compile time type safety and correctness.

Re:Why I like Golang's interfaces by realharo on Thursday April 13, 2017 @12:34PM (#14108520)
That would be similar to an intersection type (https://www.typescriptlang.org/docs/handbook/advanced-types....), but due to the way Go works, it cannot contain duplicates, and it doesn't work for structs. Union type is "it can be one of these types", and you can then access only fields/methods that all of those types share (unless you cast them of course). The naming can be a bit counter-intuitive.
Re:Why I like Golang's interfaces by hiker on Thursday April 13, 2017 @12:32PM (#14108497)
Not really, it's equivalent to a product type.
Re:Why I like Golang's interfaces by Drup on Thursday April 13, 2017 @06:29PM (#14111367)
Since the author likes both F# and structural typing, he might be interested in OCaml. F# took a lot of stuff from OCaml, but avoided both the object system and most of the module system, which both feature different flavor of structural typing.

In both case, as opposed to Go interfaces, they are actually safe (downcasting is forbidden, upcasting is checked at compile time) and the OCaml language has very good type inference, type annotations are not needed.

Re:Why I like Golang's interfaces by micah_chatt on Thursday April 13, 2017 @09:42AM (#14106772)
The thing that tripped me up was you can't pass lists of interfaces across function boundaries. It's confusing when you're starting out
Re:Why I like Golang's interfaces by zellyn on Thursday April 13, 2017 @11:06AM (#14107574)
Could you post an example of what you're talking about? Passing slices of interface values works just fine. https://play.golang.org/p/9CWzdnRb8U

Is this what you mean? Trying to pass a []fooint as a []Fooer doesn't work: https://play.golang.org/p/dmFfr0Exnk - that doesn't work in many languages, which is sad, but is for reasons that make sense if I think about what it would actually be doing under the covers: an interface value needs to store type information and/or a pointer to some kind of function pointer table, so you'd have to recreate that for each list element if you change the type of the list.

Re:Why I like Golang's interfaces by adonovan on Thursday April 13, 2017 @12:17PM (#14108319)
Also, a []Fooer supports the operation of storing any element assignable to Fooer, whereas a slice []fooint does not support that operation, so it makes sense that a []fooint cannot be used anywhere a []Fooer can.
Re:Why I like Golang's interfaces by zellyn on Thursday April 13, 2017 @01:00PM (#14108778)
That is a nice way of putting it.
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. :)
Re:Why I like Golang's interfaces by camus2 on Thursday April 13, 2017 @01:05PM (#14108836)
> that doesn't work in many languages,

it does when the language has generics. You in fact wouldn't need 2 types []fooint and []Fooer but a single List<T Fooer> , as long as fooint implements Fooer.

So with Go you end up writing functions that convert types manually. The 1000's time you do this, it starts getting a bit tiresome.

Re:Why I like Golang's interfaces by zellyn on Thursday April 13, 2017 @01:14PM (#14108933)
Well, Java has generics, but you still can't do this same thing: http://stackoverflow.com/questions/1115230/casting-object-ar...

I agree that having generics in Go would be fantastic, and I'm excited at rsc's articulation of why. But they don't completely solve this problem.

Re:Why I like Golang's interfaces by camus2 on Thursday April 13, 2017 @01:20PM (#14109004)
But with Java the point is that you'd approach the issue differently without being overly verbose just like Go. I mean my example is clear. You wouldn't have to cast anything provided you use generics at first place.
Re:Why I like Golang's interfaces by eximius on Thursday April 13, 2017 @10:44AM (#14107359)
Have any go developers run into issues where an interface is satisfied where it shouldn't be due to name conflicts?
Re:Why I like Golang's interfaces by gjem97 on Thursday April 13, 2017 @11:05AM (#14107568)
Technically it happens occasionally, especially if you prefer terse method names and/or small interface definitions. However I have yet to see an example of it causing an issue. Usually I see problems where a struct doesn't satisfy an interface where it "should".
Re:Why I like Golang's interfaces by echlebek on Thursday April 13, 2017 @11:11AM (#14107637)
I've been developing Go apps for 4 years now. So far this has never happened to me. I do agree it's a possibility, if a callee was to do a type assertion or something.
Re:Why I like Golang's interfaces by zellyn on Thursday April 13, 2017 @10:58AM (#14107484)
Not really. Can you describe a situation where accidentally satisfying an unrelated interface would cause a problem?

The worst I can think of are possible developer confusion, and maybe your autocomplete suggesting the wrong thing.

Re:Why I like Golang's interfaces by ryancouto on Thursday April 13, 2017 @10:53AM (#14107437)
what do you mean?
Re:Why I like Golang's interfaces by zellyn on Thursday April 13, 2017 @10:56AM (#14107464)
I think they mean accidentally satisfied interfaces.
Re:Why I like Golang's interfaces by ryancouto on Thursday April 13, 2017 @11:01AM (#14107516)
i was hoping they'd have an example to point to, can't imagine this being a huge problem but i'd be happy to be proven wrong.