News for hackers. Stuff that matters.
Re:My Go Resolutions for 2017 by i_don_t_know on Wednesday January 18, 2017 @01:23PM (#13428791)
I think they thought of the standard OOP solution to algebraic data types. Rather than pattern match on the value and then do the work, you dispatch on the value through an interface and do the work. I don't know go so here's some pseudo code:

    type Foo = { x : int }
    type Bar = { y : string }

    interface DoStuff { doStuff() -> void }

    function Foo.doStuff() {
        ...do stuff with Foo.x
    }

    function Bar.doStuff() {
        ... do stuff with Bar.y
    }

    function main() {
        value := getSomethingThatImplementsDoStuff()
        value.doStuff()
    }
Compare this to ML:

    type DoStuff = Foo of int | Bar of string

    let main =
        let value = getSomethingThatReturnsDoStuff ()
        match value with
        | Foo of x -> ...do stuff with x
        | Bar of y -> ...do stuff with y
Re:My Go Resolutions for 2017 by pcwalton on Wednesday January 18, 2017 @02:35PM (#13429568)
This is basically the expression problem: https://en.wikipedia.org/wiki/Expression_problem

The expression problem is motivated by the fact that the OO style (which Go favors via interfaces) makes adding new cases easy (just add another subclass) but adding new methods hard (you have to modify every subclass), while the pattern matching style makes adding new methods easy (just add another match statement) but adding new cases hard (you potentially have to modify every match statement in the program). Depending on the task at hand, either one may be more advantageous. The solutions to the expression problem are attempts to get the best of both worlds.

A lot of programming language designs deny that this is a tradeoff, which I think is misguided. Sometimes it's more convenient to be able to add methods easily, and sometimes it's more convenient to be able to add cases easily.

Re:My Go Resolutions for 2017 by treehau5 on Wednesday January 18, 2017 @04:29PM (#13430451)
My understanding is go's interfaces is counter to OO style. It's about behavior, not objects, plus by recommending to users that you write the smallest possible interface as you can, and then compose other interfaces out of those smaller one's, you reduce the surface area of where the expression problem can come into practice!
Re:My Go Resolutions for 2017 by pcwalton on Thursday January 19, 2017 @03:39AM (#13434141)
Go's interfaces are pure subclassing for the purposes of the expression problem.
Re:My Go Resolutions for 2017 by zaphar on Wednesday January 18, 2017 @10:56PM (#13432872)
Substitute implementation for subclass and the point still stands.
Re:My Go Resolutions for 2017 by brandonbloom on Wednesday January 18, 2017 @01:29PM (#13428852)
Indent 4 spaces for code.

Here's another way to do this in Go:

    func f(obj interface{}) {
            switch obj := obj.(type) {
            case Foo:
                    // Do stuff with obj.x
            case Bar:
                    // Do stuff with obj.y
            default:
                    return fmt.Errorf("obj is of unexpected type: %T", obj)
            }
    }
Re:My Go Resolutions for 2017 by weberc2 on Wednesday January 18, 2017 @02:10PM (#13429323)
Unfortunately, this makes a runtime error from something that could be caught at compile time.
Re:My Go Resolutions for 2017 by adrusi on Wednesday January 18, 2017 @03:06PM (#13429811)
It's possible to make closed interfaces in Go (give it an uncapitalized method), so there could be a compile-time warning when you don't handle all possible cases in a type switch on a closed interface.
Re:My Go Resolutions for 2017 by weberc2 on Wednesday January 18, 2017 @08:47PM (#13432274)
Maybe this is possible (and I'm not sure it is--this seems like the sort of thing struct embedding would break), but it still doesn't fix that interfaces incur an alloc. Further, it's a shame to abuse interfaces like this; it's not very ergonomic to define a method on a type to denote its membership in a set when we could do something like `type Foo enum { int, Bar, string, []byte }`. None of this is to say implementing real enums in the language is an easy endeavor; only that interfaces are a poor substitute.
Re:My Go Resolutions for 2017 by jerf on Wednesday January 18, 2017 @11:26PM (#13433008)
It is possible, and struct embedding does not break it because struct embedding only enables composition. There is no inheritance, so you can not embed a struct that has something private in it and then "inherit" it and override the private stuff. There is no such thing as inheritance in Go. You can "decorate" methods (in the GoF patterns sense), but the internal implementation that you are decorating only gets the instance of itself, not the decorating class.

I'm not addressing your other points, just your first sentence, since that is a matter of fact I can concretely address.

Re:My Go Resolutions for 2017 by weberc2 on Wednesday January 18, 2017 @11:43PM (#13433072)
> struct embedding only enables composition

Not sure what you mean here. Struct embedding is composition. It enables delegation of methods, which allows the outer struct to satisfy the same interfaces as the inner struct. This necessarily means you can't create closed interfaces. See for yourself: https://play.golang.org/p/akn9rir5of

This holds even if the outer and inner structs are defined in different packages.

Re:My Go Resolutions for 2017 by jerf on Thursday January 19, 2017 @02:16AM (#13433782)
This actually sent me down quite the path of discovery, which yielded to me a couple of surprises which I may have to document somewhere else. But the answer to your question is that you can seal an interface by declaring a private interface that has a private method. If you do that, nobody can construct any type that will implement that, even if they peek in the module and look at the private method name. The compiler will yield up something like:

    src/b/b.go:16: cannot use b (type B) as type 
       a.private in argument to a.CallPrivate:
            B does not implement a.private (missing a.privatemethod method)
                    have privatemethod()
                    want a.privatemethod()
A public interface with a private method does behave a bit interestingly. You can create new structs that compose in the implementation, and if you have a function (not method, function) in the private package that tries to type match on them, it can indeed end up with a type unknown to your package. However, you still have not necessarily "opened the interface" because it remains impossible to ever call a private method on that interface that was not defined in the original package. I haven't tested what public methods do.

I was able to construct a struct that in some sense had two different methods which could both be called under different circumstances with the same short method name, which is a bit weird.

So, in Go, you can completely close an interface such that you'll never get a type you don't expect, or you can close an interface in a way that means you may get an underlying type you didn't expect but the method will still be guaranteed to come from your package.

Re:My Go Resolutions for 2017 by weberc2 on Thursday January 19, 2017 @10:12AM (#13436348)
I believe that you can seal an interface by making it and its method(s) private, but that doesn't do us much good because we're talking about using this to build something that looks like an enum (complete with compiler warnings when we fail to address a branch in a type switch). If you only intend to use your "enum" in the package, then the interface isn't sealed in the scope you want to use it. If you want to use the enum outside of your package, then you can't use the private interface at all.

So you can create a "sealed interface" only in the sense that you can't use it at all outside of its defining package, and it won't be sealed inside of its defining package. I can't see a way to build an enum from this in the way the OP described.

Re:My Go Resolutions for 2017 by i_don_t_know on Wednesday January 18, 2017 @01:32PM (#13428901)
Thanks for the formatting help!