4 ms·
So... is there a difference between structural typing and duck typing?
by twofornone 5y ago
So... is there a difference between structural typing and duck typing?
- wk_end 5y agoStructural typing is enforced at compile time, duck typing is "enforced" at runtime (by going up in a burst of smoke if you provide something that doesn't quack like a duck).
- ninkendo 5y agoDuck typing can still be a compile-time thing… C++ templates are “duck typed” in that the template can’t specify the bounds for the passed-in type, but if you pass the wrong type, it still breaks at compile time instead of runtime. (It’s just that the error messages are way more impenetrable than they otherwise could be.)
- wk_end 5y agoI’d argue that either “runtime is compile time in the case of C++ templates” and/or “C++ templates are structurally typed”, especially now that concepts are a thing. ETA: and/or that talking about C++ templates as "typed" one way or another is problematic, given that they're a part of the type system.
- RedNifre 5y agoI'd say structural typing is static typing (you declare the shape you want upfront), whereas duck typing is dynamic typing (it works as long as it fits, as soon as it doesn't it crashes).
- austincheney 5y agoJust to ensure we are talking about the same things I am using these as definitions: * https://en.wikipedia.org/wiki/Structural_type_system https://en.wikipedia.org/wiki/Structural_type_system * https://en.wikipedia.org/wiki/Duck_typing https://en.wikipedia.org/wiki/Duck_typing These are non=competing qualities. In duck typing there is no actual code interpretation, as in run time evaluation. Instead instances are compared against a type definition looking for conformance and presumed good so long as the compiler does not see a misalignment to the type definition. This is generally good enough when both the type definitions and the code instances are static qualities in the code, but exceptions can occur if there are dynamic mutations the compiler does not recognize such as extending extending a base type with a dynamic quality. Structural typing uses structures to define relationships and those relationships become points of evaluation to determine subsets. Relational evaluation is most useful in tree models such as DOM (HTML, XML), file system, certain database models like DB2, some machine learning models. The useful qualities of relationship evaluation are that you can evaluate correctness via a path, as in navigation from one node to another, and determine if a code instance is properly composed. There is some overlap between those two type definitions in TypeScript in that an object interface can be defined from various smaller interfaces. This is mostly done for code reuse, but benefits composition.
- akaptur 5y agoYou might be interested in the PEP introducing structural subtyping to Python: https://www.python.org/dev/peps/pep-0544/ https://www.python.org/dev/peps/pep-0544/ - there, they use "structural subtyping" and "static duck typing" as synonymous.