6 ms·
The D people laugh at the dynamic type languages, yet they have "auto" in-front of all variables ;)
by z3t4 11y ago
The D people laugh at the dynamic type languages, yet they have "auto" in-front of all variables ;)
- qznc 11y agoInfered type is different than dynamic type.
- coldtea 11y agoAuto is just a keyword to invoke/assist type inference in the parser -- the objects are still statically typed. C++ 11x (or however it's called) uses the same. You were probably thinking of something like the "dynamic" type that C# has.
- thechao 11y agoSince Jaakko, my advisor, was one of the core authors for 'auto' in C++11: the use of 'auto' for type inference has been in C since the '70s. C++11 just generalized the feature.
- coldtea 11y agoHuh? Maybe it's a joke, but "auto" in C has nothing to do with auto in C++. In C auto just defines the storage location (and is kinda useless), not the type (which you still need to define).
- thechao 11y agoKind of serious-kind of joking: auto x = 3; "Infers" 'int'. Everything else is an error.
- Drup 11y agoAnd apparently, you have no idea what type inference is. ;)
- geofft 11y agoTo 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.