3 ms·
Well, the problem is on annotating types, when you only actually care about behavior. Duck typing isn't a bad thing. In fact, it's great. You just need to anno
by marcosdumay 11y ago
Well, the problem is on annotating types, when you only actually care about behavior.
Duck typing isn't a bad thing. In fact, it's great. You just need to annotate the ducks instead of the types. For a great example of how to do it, look at Haskell's type classes.
(In fact, Haskell's easyness of integrating generic code is visible even to a beginner, but I never notice that this is because the entire language is based on duck typing.)
- jstimpfle 11y agoWell... either your library invents its own type classes, and all client code must implement them (boilerplate!). Or it relies on agreed-upon type classes which come from another library. If it's not basic classes like Functor or Monad that's just not manageable (dependency hell!).
- marcosdumay 11y agoDependency hell is the situation where A depends on both B and C, but B depends on D, and C depends on D', where D and D' are not compatible. Type classes absolutely do not create this kind of situation, as they are duck typing. But modules do create it, so it happens both on Haskell and on Python. Now, in practice you use both, basic classes and library specific ones. And yes, if you want to create some behavior, you'll need to implement the specific code that enables it. You do not escape from that in Python either.
- jstimpfle 11y agoI know dependency hell when I see it. It's already bothersome if I can't just apt-get install the right library version because another package depends on a conflicting version, or the latest boost version isn't available yet. I refuse to learn tools like pip or stack for every language I use, and to create sandboxes for new projects only to get the latest library versions. That's just never going to be portable software. You can actually escape dependency hell for most APIs. That's the Unix culture of using plain text (read: very simple data types -- in C that would be arrays and structures of integral types) for communication. Of course that won't work for libraries that provide transformations on parameterized structures. But let's face it, there's no point in depending on a Monad library just to save two lines of pure code.
- thristian 11y agoYeah, I immediately thought of Rust traits, which I hear are very like typeclasses. Python already has something like these, in the form of Abstract Base Classes: collections.Callable for function-like things, collections.Iterable for things that conform to the iterator protocol... But if gradual typing is to be useful, it really needs ABCs for all the common ducks that Python code uses: file-like objects, things that dict() can convert into a dictionary, things that can be converted into strings...
- justanotherbody 11y agoI'm not sure any committee could agree as to what defines a filelike object (does it implement seek?) In addition you'd need an IRO (interface resolution order) if you truly intended to prove everything would work To say nothing of accidentally implementing an interface and getting an error because if it Doesn't sound like my kind of python...
- Retra 11y ago>I'm not sure any committee could agree as to what defines a filelike object (does it implement seek?) You step back a layer of abstraction. Maybe it implements seek, maybe it doesn't. Either way it implements has_seek.
- aidenn0 11y agoYou have a seekable filelike interface that is a superset of the base filelike interface.
- aplusbi 11y agoTo be pedantic, Haskell technically doesn't use duck typing, it uses structural typing. Duck typing is run-time coercion of types, structural typing is compile-time inference of minimum interfaces.
- marcosdumay 11y agoNoticed. Interface based typing may be a better generic term.