4 ms·
Flow types (for JavaScript) can be complied into run-time type-checks. Properly statically typed languages do not need run-time checks because the compiled cod
by de_keyboard 5y ago
Flow types (for JavaScript) can be complied into run-time type-checks.
Properly statically typed languages do not need run-time checks because the compiled code is already proven to be valid by the type-checker.
- zohch 5y ago> Flow types (for JavaScript) can be complied into run-time type-checks. Okay, so there are some, though flow is pretty obscure, and flow-runtime (which is the part which does this AFAICT) is even more obscure. I don't think this is a popular feature. > Properly statically typed languages do not need run-time checks because the compiled code is already proven to be valid by the type-checker. If you want that, just enable strict mode for mypy. And if you want to interface with code that is not stirctly typed, just use runtime type checks, which will inform the static type system: foo: Any = untyped_code() assert isinstance(foo, MyClass) # after this mypy will treat foo as instance of MyClass. Same can be done with other types of checks also. So if you want runtime type checking that informs the type system, you have it. I certainly don't want runtime checks for what the type system has already checked for me.
- breuleux 5y ago> If you want that, just enable strict mode for mypy. But I can't do that if I want to do gradual typing. If I type a function, but the type system cannot figure out what's going to call it, that's when I want it to perform a check: at the boundary. > I certainly don't want runtime checks for what the type system has already checked for me. The type system can remove the checks if it can prove that they always pass. My point is that if it cannot prove that they will pass, I want them at runtime.
- zohch 5y ago> But I can't do that if I want to do gradual typing. If I type a function, but the type system cannot figure out what's going to call it, that's when I want it to perform a check: at the boundary. Unless you start using # type: ignore - the only way to interface with untyped code will be to use asserts or other checks (if + throw), i.e. perform checks at the boundary.
- G3rn0ti 5y ago> Properly statically typed languages do not need run-time checks Well, what's a properly compiled language these days? Golang e.g. uses run-time dispatching due to the way interfaces work and, of course, garbage collection. Even C++ needs to do run-time dispatching of virtual methods. So the only languages left these days that do not use run-time checks are probably C and Fortran.
- de_keyboard 5y agoI mean run-time checks that the user must implement. For example, in JS you might put these all over your code-base: if (typeof x !== 'string') { throw Error('Expected a string'); }
- zohch 5y agoThis will already inform mypy that after that if statement x is type string.