3 ms·
The big advantage of dynamic typing is that your code only needs to be correct on the code paths that are actually taken. That's important for the web, which is
by millstone 13y ago
The big advantage of dynamic typing is that your code only needs to be correct on the code paths that are actually taken. That's important for the web, which is full of browser specific hacks, polyfills, feature detection, etc.
Consider the difficulty of doing static type checking for a property like window.localStorage. Its type depends on the runtime environment, and may not exist at all. So static type checking against one browser environment doesn't give you much assurance that your code will type check in other browsers. You really do need to do type checking at runtime.
- EpicEng 13y agoYeah that's what polymorphism is for. window.localStorage would just be the interface, the underlying concrete type is irrelevant.
- millstone 13y agowindow.localStorage doesn't exist in (for example) IE 7, and it may or may not exist in IE8 depending on the domain. So you can't rely on the interface being the same.
- EpicEng 13y agoPlenty of libraries written in statically typed languages add features which may not be supported on legacy platforms. There are ways to deal with this. In version X you provide a way to let the client know that function Y is not supported on the current platform. It's not like the web is some completely different animal, it's just an example of poor/little standardization and a design which has not evolved well over time.
- millstone 13y agoMy point was not that static typing makes this stuff impossible, just more awkward and difficult.
- ilaksh 13y agoThe big advantage of dynamic typing is that you don't have to specify it, so your code is easier to write and easier to read.