3 ms·
To give a little more detail on the difference between type inference and dynamic typing, the following code is valid in a dynamic language, but a static langua
by geofft 11y ago
To give a little more detail on the difference between type inference and dynamic typing, the following code is valid in a dynamic language, but a static language rejects it:
auto x = 15;
if (some_condition) {
x = "Hello world!";
}
print(x);
And this solves real errors. I believe there's even a function somewhere in the Python standard library that, if it finds one result, returns a string, and if it finds multiple results, returns a list of strings. Of course a string is itself iterable, so duck-typing goes horribly wrong.
- scardine 11y agoI'm OK with dynamic types, they buy you something, cost you something. My big complaint is weakly typed languages (I'm looking at you, JavaScript). I've just fired SHIFT+CTRL+i and typed the line below in the JavaScript console: > (1 + "1") * 2 22 At least it raises a TypeError in Python (unsupported operand type(s) for +: 'int' and 'str'). OK, probably better to fail at compile time, but I would rather fail at runtime than not fail at all.
- z3t4 11y agoIn the early days of my JavaScript'n I always used minus instead of plus when doing addition. But as the language and tools has matured I hardly see any type errors any more. I do have a habit of parseInt() that won't easily go away though :P Besides the concatenation of numbers to strings and vice versa, most other type errors in JavaScript will give you a syntax error or something like NaN (Not a Number). So don't mix strings and numbers. Everything else is just "objects". =) One thing that causes lots of bugs though is undefined object properties. But I'm not sure if inferred types would help in those cases.