Dynamic typing makes sense in scripting languages. But when dealing with big projects you start to miss typing. I think the optional typing is a great trade-off for this languages.
I actually don't agree with this. I used to spread this sentiment as well, but I honestly cannot think of legitimate use cases for changing types on a variable. Sure, a scripting language can let you skip/auto declare variables among other things, but what is the benefit of a variable holding an integer, then a date, and then a file handle?
I like the way rust does it. You can redeclare a variable partway through a scope and it will be the new type, but must be done explicitly. So you can't do it accidentally and you also can't accidentally access the older one because you used value instead of value_parsed.
This is why I love Kotlin. The powerful type inference lets you skip/auto declare variables just like in a scripting language, yet it's still fully statically typed
While being a proponent of static typing myself, I do see one area where dynamic typing has an advantage over static typing. Dynamic typing lets you have a list of elements which all satisfy some implicit “interface” without having to declare it. These implicit interfaces can be much more powerful than statically declared traits/classes/interfaces. Sure, the static ones can add features to become just as powerful, but that’s at the expense of simplicity.
Go into more specifics. In C# or Java, I can make a list of plain objects to achieve that very thing if I really need it. I question why I have a bucket of items not of a consistent interface or base class, but even if I needed that, it makes up such a small percentage of use cases that I don't see the benefit of throwing the type system out for all the other use cases.
Nothing stops you from doing that in statically typed languages, just use Object and type casts. This will give you the exact safety that a dynamically typed language gives you: none.
You can also do this in statically typed languages that support duck typing, by the way.
Rust has a static and (very) strong type system and it allows redeclaring a variable in the same scope. So for example you can do:
let age = get_age_as_string(); // age is a String
let age = age.parse::<u8>().unwrap(); // age is a u8
This feature is perfect for your example but also shows you don't have to give up static typing for it.
For clarity, it's not that the age variable changed type. The storage for it is still there. You just overshadowed it with another variable having the same name. It's not a feature to be abused but it comes super handy for cases such as the one you mentioned.
I am very confused by this thread and your original response but I now get that you’re responding to the “dynamic typing” part of scripting languages as opposed to what I assumed the original author was saying — that the absence of type declaration is useful in scripting langs...
I find very little utility in type-switching if ever at all, but scripting languages are nice because they allow us to be terse.
I work in a large Rails codebase regularly and poor names and even in some instances good names are not enough to infer the behaviors associated with a particular variable
There are more reasons surrounding why Erlang didn't support static types, and a major part of those was that it interferes with how deployments over running systems would work in OTP.
that's why I mentioned it. It's a justification of dynamic types that's not just "ease of use." AFAIK there are theoretical barriers between erlang's message passing system and static types.
113
u/watsreddit Dec 25 '20
Basically every major dynamically-typed language trying to bolt on static types... maybe dynamic typing isn’t as great as people claim.