News for hackers. Stuff that matters.
Re:Interfaces and Protocols in Python by neolog on Friday March 19, 2021 @04:12PM (#26517728)
> Unlike Interfaces, Protocols can describe the types of things that already exist.

Can't interfaces do that too? You can just say builtins.set implements the receiver interface.

Re:Interfaces and Protocols in Python by nine_k on Friday March 19, 2021 @10:02PM (#26520124)
Depends on the language.

In Java, you have to explicitly mark a class as conforming to an interface inside the class's declaration. You need to have the source and modify it.

In Haskell, you mark a type as conforming to an interface (called typeclass) by adding functions that operate on its instances and implement the interface's requirements. You can do it anywhere, and write an implementation for an existing type, which is normal practice.

In Python, you cannot easily add interface conformance, but possibly you can make a trivial subclass of the desired class, and mark it as conforming to the desired interface (Protocol), without modifying the source.

Re:Interfaces and Protocols in Python by zeugmasyllepsis on Friday March 19, 2021 @06:24PM (#26518737)
Not without modifying the existing type. Since Interfaces rely on nominal typing, each type implementing an interface must explicitly declare that they do so. On the other hand, since Protocols are checked structurally you can declare a protocol after several types already implement it, and type checking will pass without having to modify the implementing types.
Re:Interfaces and Protocols in Python by neolog on Friday March 19, 2021 @07:06PM (#26519071)
Explicitly declaring which types provide which interfaces is the whole point of explicit interfaces.

But I don't think the "you can declare a protocol after several types already implement it" is any different under explicit vs implicit conformance checking.

I can just say

    implementer(IReceiver)(builtins.set)
at any time. There's no temporal restriction on when I can do that.
Re:Interfaces and Protocols in Python by zeugmasyllepsis on Saturday March 20, 2021 @10:38AM (#26523516)
Right - so if you're introducing type annotations into an existing project and have a method expecting some interface, used by many call-sites spread across various parts of the project, then you can go around wrapping the original type at each call-site as you've indicated.

Or add a single type annotations to the method declaring that the parameter satisfies a protocol.

Re:Interfaces and Protocols in Python by neolog on Saturday March 20, 2021 @06:13PM (#26526429)

    implementer(IReceiver)(builtins.set)
only needs to be written once, not once at each call site. It's telling the global registry that builtins.set implements IReceiver.
Re:Interfaces and Protocols in Python by joshuamorton on Friday March 19, 2021 @06:03PM (#26518586)
You have to say that builtins.set implements the interface. If there's a receiver protocol, it'll just accept a set of that matches the protocol, no registration required.