3 ms·
Actually, I believe this is expressible in Python's type system: # Python 3.8 from typing import Protocol, TypeVar T = TypeVar('T') class Sum
by anentropic 6y ago
Actually, I believe this is expressible in Python's type system:
# Python 3.8
from typing import Protocol, TypeVar
T = TypeVar('T')
class Summable(Protocol[T]):
def __add__(self, other: T) -> T: ...
In Python 3.4-3.7 you'd need to install typing-extensions library:
from typing_extensions import Protocol
- still_grokking 6y agoInteresting. It seems that "every second language" gets "type-classes" these days… C++ added such feature, this above looks like a Python version, Rust has it, Scala had them as one of the first "OOP" languages. It's nice to see that Haskell pioneered a very useful and now spreading language feature!
- sacado2 6y agoAda had them before Haskell, and I'm not even sure it was the first language with them.