News for hackers. Stuff that matters.
Re:Half a decade with Go by arnehormann on Tuesday November 11, 2014 @04:37AM (#8589060)
Because of the implicit thing! The api designer doesn't have to write the interface, you can do it yourself. As long as naming conventions are kept to and the signature matches, you can apply this anywhere.

Imagine a close() interface in Java. There isn't one - but having a try {...} finally { x.close(); } can be very useful sometimes. But having interface graphs made of granular interfaces makes everything slow and causes a lot of complexity when you try to think about your type hierarchy. That's why interfaces always just grow and you can't use them any longer because other classes only implement part of the api. Something usable for all of awt, swing, file handling, random foreign libraries? Unthinkable. Also, adding something in a later release (like CharSequence in 1.4) can have a wide ranging impact and requires you to change a lot of code.

In Go, you just add an interface from the union set of multiple structs api - and you can use it. No matter who wrote those structs. The value is enormous. Think of it as something like dependency injection at compile time.

Re:Half a decade with Go by mschulze on Tuesday November 11, 2014 @06:45AM (#8589365)
>Imagine a close() interface in Java. There isn't one

https://docs.oracle.com/javase/7/docs/api/java/lang/AutoClos...

You can even leave out the finally when you use an autocloseable resource.

    //r will be closed no matter what
    try(Resource r = getResource()) {

    } catch(SomeException e) {

    }
Re:Half a decade with Go by arnehormann on Tuesday November 11, 2014 @07:13AM (#8589440)
My Java is getting rusty... I read about this sometime somewhere but forgot it, thanks! I should have picked something like "String getText()", then.
Re:Half a decade with Go by pjmlp on Tuesday November 11, 2014 @05:53AM (#8589249)
> Because of the implicit thing!

Known as structural typing and available in most modern languages.

Re:Half a decade with Go by icebraining on Tuesday November 11, 2014 @06:27AM (#8589319)
available in most modern languages

Well, "modern" is an ill-defined concept. As far as I'm aware, structural typing is not really that common, is it? Besides OCaml and Scala, is there any relevant (used outside of academia) language that supports it?

Re:Half a decade with Go by pjmlp on Tuesday November 11, 2014 @06:50AM (#8589378)
D and C++ templates for example.

F# also supports it, given its ML linage.

C# tricks with dynamic, although in this case it is dynamic typing, so not really the same thing.

Re:Half a decade with Go by seabee on Tuesday November 11, 2014 @07:14AM (#8589441)
The disadvantage of C++ templates is the structural type is implicit - you only know if the input object satisfies the type if you read the documentation, code, or can decipher the error message that occurs if it didn't.

Concepts would have fixed this, but we don't have concepts and maybe never will!

Re:Half a decade with Go by pjmlp on Tuesday November 11, 2014 @08:20AM (#8589626)
> Concepts would have fixed this, but we don't have concepts and maybe never will!

enable_if and type traits are a workaround for the time being.

Concepts lite will definitely be in the next revision.

Re:Half a decade with Go by losvedir on Tuesday November 11, 2014 @08:08AM (#8589588)
Would rust's "traits" count as structural typing? (I know Rust may not count as "relevant (used outside of academia)" yet, but maybe in the future.)
Re:Half a decade with Go by tel on Tuesday November 11, 2014 @08:51AM (#8589734)
Haskell's typeclass provide it.
Re:Half a decade with Go by dragonwriter on Tuesday November 11, 2014 @10:28AM (#8590242)
Typeclasses aren't structural typing, they are nominative typing, as typing is controlled by explicit declaration of relations between types and typeclasses, not inferred from structural properties.
Re:Half a decade with Go by tel on Tuesday November 11, 2014 @11:12AM (#8590533)
Sure they are: (forall a. X a => a) is a supertype of (forall a. (X a, Y a) => a).
Re:Half a decade with Go by dragonwriter on Tuesday November 11, 2014 @09:28PM (#8593676)
I stand corrected.
Re:Half a decade with Go by raverbashing on Tuesday November 11, 2014 @04:53AM (#8589102)
Like duck typing?
Re:Half a decade with Go by LBarret on Tuesday November 11, 2014 @05:40AM (#8589222)
I suspect this is like duck typing with some verification before compilation : the compiler checks that the object sent as parameter implements the correct interface.
Re:Half a decade with Go by diroussel on Tuesday November 11, 2014 @06:03AM (#8589273)
It's called structural typing.
Re:Half a decade with Go by kaoD on Tuesday November 11, 2014 @07:51AM (#8589537)
So, compile-time duck typing.
Re:Half a decade with Go by dllthomas on Tuesday November 11, 2014 @10:07AM (#8590117)
So, wikipedia says (http://en.wikipedia.org/wiki/Duck_typing#Structural_type_sys...):

"Duck typing is similar to but distinct from structural typing. Structural typing is a static typing system that determines type compatibility and equivalence by a type's structure, whereas duck typing is dynamic and determines type compatibility by only that part of a type's structure that is accessed during run time."

So it sounds like "duck typing" is defined in terms of run-time semantics. I guess you might be able to have genuine "compile-time duck typing" in a dependently typed language, which could be a good reason to preserve the distinction.

Otherwise, yes, they are certainly similar.

Re:Half a decade with Go by tel on Tuesday November 11, 2014 @08:52AM (#8589740)
In the same Formula 1 is performance go-karting.
Re:Half a decade with Go by coldtea on Tuesday November 11, 2014 @08:55AM (#8589749)
No, structural typing. It has a name already.