Op here back from the dead. This is in fact not a stab at dynamically typed languages, or at least not only: statically typed languages such as Java also support this kind of construct. In fact, one could develop a technically type safe programming language where an
instanceof
construct has sound semantics.What
instanceof
breaks is something called polymorphic parametricity, i.e. the fact that generic functions don’t know anything specific about the types they are generic over. This is the fundamental condition for what in the community is dubbed “theorems for free”, that is, naturality of generic functions between generic types.This is referencing Philip Wadler’s 1989 paper “Theorems for Free”, which is fairly well known in the Haskell community: https://home.ttic.edu/~dreyer/course/papers/wadler.pdf
What
It’s making fun of dynamic languages because rather than letting the compiler prove theorems about statically typed code, they… don’t.
Dynamic languages were invented by runtime error companies to sell more runtime errors.
Turns out getting working code is a lot cheaper and more useful than formally proven code.
Cheaper? Yes, I guess so, depending on how you measure cost. More useful? Absolutely disagree.
Industry will pick functionality over verification every time.
Industry will choose not to verify that your function does not produce NullPointerException wasting hours of the client’s work, because in order to do that they would have to have actual requirements for software developers, and in order to do that they would have to 1 - have the managers be actually technically literate, and 2 - pay the developers properly That’s it. That’s the theorems. The “formal verification” we’re talking about here are those of the likes of “this value is a damn integer”, or as you could interpret it “your code is not stupidly broken”.
To be clear, I’m not writing this big comment for you, I know you’re trolling or whatever you’re into, I’m writing this to inform other readers. ✌🏻
Industry will leak PII without consequence every week.
I need an explainer on this one
if (theorems_for(free)) make_instance_of(x, String)
What
I’m sorry, I’m only a novice Python guy. Know enough to get two RESTful APIs to talk to each other and do some network automation or rudimentary Ansible plugins.
What’s wrong with
if isinstance(x, str):
?Apparently, “Theorems for free!” is a paper that talks about an extensive ability to reason about parts of programs, if you follow some rather basic rules.
However, lots of popular programming languages throw this ability out the window, because they do not want to enforce those basic rules.
Most languages, for example, allow for rather uncontrolled side effects and to be able to reason as a programmer, you have to make the assumption that no one else abused side effects.The
instanceof
is rather referring to dynamic typing, though, as e.g. employed by Python and JS, which makes it difficult to make any assumptions at all.So, in statically typed languages, when you’re implementing a function, you can declare that a given parameter is a number or a string etc. and the compiler will enforce that for you. In dynamically typed languages, you have to assume that anyone calling your function is using it correctly, which is a difficult assumption to make after a refactoring in a larger codebase.
All in all, such different levels of rigorosity can be fine, but the larger your codebase grows, the more you do want such rules to be enforced, so you can just ignore the rest of the codebase.