3 ms·
Honestly, I always felt a bit lost in the whole “static vs dynamic types” debate because I dislike how it’s framed. So “a language is statically typed if you kn
by LudwigNagasena 4y ago
Honestly, I always felt a bit lost in the whole “static vs dynamic types” debate because I dislike how it’s framed. So “a language is statically typed if you know its type at compile-time”. Ok, so you have ParentClass and ChildClassA and ChildClassB that inherit from it. And you create a variable that holds objects of the type ParentClass. But do you know at compile-time whether it is ChildClassA or ChildClassB? Only if your type inference system is strong enough and your code is structured appropriately for such possibility to even exist. And do you even need to know it? Most likely not unless that one time when you do in which case you use some ugly reflection tricks and whatnot. And, bam, now your “statically typed” language actually has dynamic type checking.
And if you use a “statically typed” language, but have an algebraic type that holds int or string or ClassA or Array, how useful is that static typing? That’s why I prefer calling the code either weekly typed or strongly typed. And some languages allow you to have strongly typed code with ease, and some languages allow you to have weakly typed code with ease, and some, like TypeScript, allow you both in which case the whole dilemma seems unwarranted or at least outdated.
- masklinn 4y ago> Ok, so you have ParentClass and ChildClassA and ChildClassB that inherit from it. Why do you even assume classes are involved? > And, bam, now your “statically typed” language actually has dynamic type checking. Not in any way, shape, or form? You have a ParentClass, it lets you perform ParentClass operations. If you want to do a ChildClassA operation if that's that, you check whether that's what you have, then do the operation. Both are enforced by the compiler (though how safe the check is varies). If you have a dynamically typed language, you might think you have a ParentClass but you actually have a completely unrelated ToyotaSupra. > And if you use a “statically typed” language, but have an algebraic type that holds int or string or ClassA or Array, how useful is that static typing? Extremely. Because you know what the potential is, and you have to check for it exhaustively. > That’s why I prefer calling the code either weekly typed or strongly typed. So complete nonsense to make you feel good?
- pwdisswordfish9 4y ago>> And, bam, now your “statically typed” language actually has dynamic type checking. > Not in any way, shape, or form? https://en.cppreference.com/w/cpp/language/type#Dynamic_type https://en.cppreference.com/w/cpp/language/type#Dynamic_type https://en.cppreference.com/w/cpp/language/dynamic_cast https://en.cppreference.com/w/cpp/language/dynamic_cast https://en.cppreference.com/w/cpp/language/typeid https://en.cppreference.com/w/cpp/language/typeid It’s there in the name.
- masklinn 4y ago> It’s there in the name. In the same way that the Democratic Republic of North Korea is democratic. Right there in the name. Also that Dynamic Programming involves not knowing anything. Right there in the name. A Dynamic Microphone? Not actually a microphone at all.
- sparkie 4y ago`dynamic_cast` does a dynamic type cast (a downcast). It does what it says on the tin. It's purpose is to cast a value whose precise type may not be known until runtime.
- LudwigNagasena 4y ago> Why do you even assume classes are involved? Because classes are almost ubiquitous in modern commercial programming? > If you want to do a ChildClassA operation if that's that, you check whether that's what you have, then do the operation. Both are enforced by the compiler (though how safe the check is varies). Go to GitHub and search for "GetType language:csharp" or "getClass language:java" and see millions of hits for runtime checking in “statically typed languages". And who knows how many ad hoc runtime checks there are in C++ and other languages without a default reflection framework. > If you have a dynamically typed language, you might think you have a ParentClass but you actually have a completely unrelated ToyotaSupra. So is a dynamic language that supports type annotations in comments (eg JavaScript + JSDoc) actually a statically typed language? > Extremely. Because you know what the potential is, and you have to check for it exhaustively. It’s still going to be a mess that should be refactored. > So complete nonsense to make you feel good? You can do runtime type checks in “statically typed” languages. You can use type inference in “dynamic” languages. That is just facts.
- sparkie 4y agoYes, downcasting performs dynamic type checking. There are no two ways about it. C++ correctly calls this `dynamic_cast`. The only way to completely avoid dynamic type checking in such object hierarchies is to forbid downcast completely, as OCaml does, for example. Most "statically typed" OOP languages have this escape hatch for dynamic typing because it is very useful, and is usually only performed after a check for the correct type has been performed. or if it is known by the programmer that the correct type is present, but there is no way to express this knowledge in the type system.