News for hackers. Stuff that matters.
Re:Interfaces Generally Belong with Users by jstimpfle on Saturday November 23, 2019 @03:50PM (#21616450)
I'm super confused what's the point here. There seems to be something about how abstract interfaces (i.e., interfaces that are separated from the implementation) are good for testing (and more?) because there is less coupling (also at build time) to other modules.

It might be hard to get for me as a C programmer because we make manually decoupled interfaces all the time (we create header files explicitly). In a sense all interfaces in C are abstract, and only the linker matches actual implementations to the interfaces. I believe with explicit header files there isn't any of the issues mentioned in the post.

Re:Interfaces Generally Belong with Users by aboodman on Saturday November 23, 2019 @04:07PM (#21616551)
Nope. The author is a C programmer too.

The post is talking about who writes the interface that client code relies on. Is it the author of a package (eg like header files) or the user of the package?

In Go and Typescript its easy for users to write their own interfaces describing just the functionality they rely on. This makes testing a lot easier.

Re:Interfaces Generally Belong with Users by aboodman on Saturday November 23, 2019 @04:10PM (#21616567)
Before someone “but Haskell”’s me:

... In Go, Typescript, and many other languages, but sadly lacking in many of today’s most popular languages...

Re:Interfaces Generally Belong with Users by hesk on Saturday November 23, 2019 @04:16PM (#21616609)
How does the compiler know that the implementing class can be used where the interface is required?

Is there still something like "Backend extends ProfilePage" in Java?

Re:Interfaces Generally Belong with Users by msbarnett on Saturday November 23, 2019 @04:52PM (#21616846)
> How does the compiler know that the implementing class can be used where the interface is required

Structural typing.

The instance does not assert to the compiler an exhaustive list of all interfaces it claims to conform to; instead, the compiler determines, during compilation, whether the concrete instance being passed meets the requirements of a declared interface. The net result is that an implementation need not have known about an interface when it was written; it need merely match the interface’s requirements when called upon to do so.

It is, effectively, statically enforced duck typing.

Re:Interfaces Generally Belong with Users by kubanczyk on Saturday November 23, 2019 @04:26PM (#21616678)
Not like "extends" or "implements".

If a class implements all the methods of the interface (as seen at compile-time - same method name, arguments, returns) then that's it, period. Compiler will allow you to use it. Beyond that the interface doesn't "know" about the class and the class doesn't "know" about the interface.

So it doesn't matter the interface didn't even exist when that class was written.

Re:Interfaces Generally Belong with Users by hesk on Saturday November 23, 2019 @04:29PM (#21616694)
Sounds a lot like Python's duck typing to me. (Although Python would coerce the types if they don't match.)
Re:Interfaces Generally Belong with Users by kevincox on Saturday November 23, 2019 @04:54PM (#21616853)
It is. "Structural typing" is basically "compile time duck typing".

One major notable advantage is that you are checking the existence of every method you may call, not just the ones your tests happen to use.

Re:Interfaces Generally Belong with Users by Too on Sunday November 24, 2019 @03:39AM (#21619165)
Javascript (without typescript) also has duck typing. The difference with structural typing is that the interface is formalized and enforced compile time.

Python 3.7 added compile structural typing using what they call Protocols. It works very similar to typescript. See https://www.python.org/dev/peps/pep-0544/

Re:Interfaces Generally Belong with Users by mcqueenjordan on Saturday November 23, 2019 @05:53PM (#21617162)
Similar in some ways, but duck typing is a misnomer because it doesn't involve typing.

These are type classes, or what Rust calls Traits.

Re:Interfaces Generally Belong with Users by aboodman on Saturday November 23, 2019 @07:56PM (#21617642)
> duck typing is a misnomer because it doesn't involve ...

dang, the joke was so close