4 ms·
Yes, it's not possible to construct a static type system that accepts all correctly-typed programs. They only accept correctly-typed programs within their type
by jnbiche 9y ago
Yes, it's not possible to construct a static type system that accepts all correctly-typed programs. They only accept correctly-typed programs within their type system.
So just because you can't devise a type system that accepts all correctly-typed programs doesn't mean that a statically-typed language can't create equivalents to any dynamically-typed program, assuming it's Turing complete. That's a misconception.
What falls under correctly-typed within most modern type systems is more than enough to write expressive programs. In fact, sometimes a static type system can be even more expressive than even the most expressive dynamic one (as is the case with Haskell, for example). As some evidence for this, I assert that one can re-create any dynamic type system within a Haskell program, using simple algebraic data types.
That's what I meant about dynamic typing not being needed. Yes, even languages like Haskell have runtime typing "escape hatches" but they're never necessary except for use with FFIs (ie, other languages) and are definitely a major code smell if you use them for normal coding. They're just not necessary.
- ridiculous_fish 9y agoWhat do you do about runtime environments that may diverge from the static environment? For example, your code may typecheck in the browser you tested, but it has to run in other browsers which have differences in their APIs. How would you gracefully handle that without dynamic types?
- Retra 9y agoIf the APIs are different, they are differently typed. You still don't need dynamic types, you need to correctly account for both APIs. Doing it gracefully requires that you say what 'graceful' means in this context. It may or may not mean doing it correctly.
- ridiculous_fish 9y agoHaving a single static type system that captures all differences across all versions of all browsers is not realistic. But even if it were, there's a bigger problem: static type checking happens too early. The browser can rev and change its APIs after your static type checking is performed. You want to type check with the actual runtime environment, not just a static guess at it. I guess by "graceful," not segfaulting would be a good start.
- sds2 9y ago> Having a single static type system that captures all differences across all versions of all browsers is not realistic. Why? Standards and abstractions are useful. > But even if it were, there's a bigger problem: static type checking happens too early. The browser can rev and change its APIs after your static type checking is performed. At that case your program's behaviour may become inconsistent > You want to type check with the actual runtime environment, not just a static guess at it. That's not typechecking and there's no guarantee that your program will work with dynamic typing if the API changes. > I guess by "graceful," not segfaulting would be a good start. Is runtime type error better? If the type of your value changes and your assumption was false about the type then it can fail anyway with an incorrect function call.
- jnbiche 9y ago> code may typecheck in the browser you tested, but it has to run in other browsers which have differences in their APIs. If you are referring to an second API that is differently typed, what happens if you do use a dynamically-typed language there? Then you're running under the assumption of one type when you actually are getting another? Your program is in an inconsistent state. Very often, at least in languages like JavaScript and Python, that means that very strange and obscure bugs will eventually start popping up. Unfortunately, the same can happen with static typed languages receiving data from an external API unless you're doing runtime checks of external APIs (which is often a good idea). So the ideal thing to do is to runtime validate external APIs and account for any differences that turn up in the APIs. Ignoring the differences in any language is very often the start of downstream bugs, not a graceful way of handling the problem.