3 ms·
Structural typing doesn’t save you unless you add other stuff to it. Example: var o = ...; // Value comes from somewhere, doesn’t matter where so long as
by pizlonator 7y ago
Structural typing doesn’t save you unless you add other stuff to it.
Example:
var o = ...; // Value comes from somewhere, doesn’t matter where so long as it’s opaque
if (p)
... = o.f
else
... = o.g
In dynamic typing, we only assert that o has f on the then, and only assert that o has g on the else.
Structural typing or abstract base classes or whatever would only save you here if you were very careful about giving o some top type and then casting. But that’s more work - so the tendency is that the static types version of this program will assert that o has both f and g at the point of assignment. And that represents a tighter coupling to whatever produces o.
- ookdatnog 7y agoYes, for that situation you additionally want sum types.