News for hackers. Stuff that matters.
Re:TypeScript for Pythonistas by twofornone on Friday December 17, 2021 @04:40PM (#29598667)
So... is there a difference between structural typing and duck typing?
Re:TypeScript for Pythonistas by wk_end on Friday December 17, 2021 @04:42PM (#29598694)
Structural 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).
Re:TypeScript for Pythonistas by ninkendo on Friday December 17, 2021 @04:47PM (#29598749)
Duck 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.)
Re:TypeScript for Pythonistas by wk_end on Friday December 17, 2021 @04:57PM (#29598853)
I’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.

Re:TypeScript for Pythonistas by austincheney on Friday December 17, 2021 @05:29PM (#29599145)
Just 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/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.

Re:TypeScript for Pythonistas by akaptur on Friday December 17, 2021 @06:54PM (#29599785)
You might be interested in the PEP introducing structural subtyping to Python: https://www.python.org/dev/peps/pep-0544/ - there, they use "structural subtyping" and "static duck typing" as synonymous.
Re:TypeScript for Pythonistas by RedNifre on Friday December 17, 2021 @04:43PM (#29598699)
I'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).