4 ms·
You realize every single thing that dynamically-typed languages can do with data types, statically-typed languages can do too? Except when it matters, they can
by chowells 3y ago
You realize every single thing that dynamically-typed languages can do with data types, statically-typed languages can do too? Except when it matters, they can also choose to do things dynamically-typed languages can't.
Lots of people assume static typing means creating domain types for the semantics of every single thing, and then complain that those types contain far more information than they need. Well, stop doing that. Create types that actually contain the information you need. Or use the existing ones. If you're deserializing JSON data, it turns out that the deserialization library already has a type for arbitrary JSON. Just use it, if all you're doing is translating that data to another format. Saying "this data is JSON I didn't bother to understand the internal content of" is a perfectly fine level to work at.
Stop inventing work for yourself.
- belmont_sup 3y agoReminds me of https://lexi-lambda.github.io/blog/2020/01/19/no-dynamic-type-systems-are-not-inherently-more-open/ https://lexi-lambda.github.io/blog/2020/01/19/no-dynamic-typ.... Always a fun revisit.
- throwaway2037 3y agoExcellent blog post. I never saw it before. Thank you to share.
- bazoom42 3y agoYou can’t do monkeypatching or dynamically modify the inheritance chain of an object in a statically typed language. But yes, you can have a JsonNode type, which is still better than having every type being “object”.
- throwaway2037 3y agoAbout monkeypatching, perhaps we have difference definitions. From time to time, I need to modify a Java class from a dependency that I do not own/control. I copy the decompiled class into my project with the same package name. I make changes, then run. To me, this is monkeypatching for Java. Do you agree? If not, how is it different? I would like to learn. Honestly, I discovered that Java technique years ago by accident. Another technique: While the JVM is running with a debugger attached, it is possible to inject a new version of a class. IDEs usually make this seamless. It also works when remote debugging. Do you consider this monkeypatching also?
- bazoom42 3y agoMonkeypatching is programatically adding a method to a class at runtime.
- chowells 3y ago> You can’t do monkeypatching or dynamically modify the inheritance chain of an object in a statically typed language. There's no theoretical reason you can't. No languages that I know of provide that combination features, because monkey-patching is a terrible idea for software engineering... But there's no theoretical reason you couldn't make it happen. I think you've conflated static typing with a static language. They're not the same thing and can be analyzed separately.
- bazoom42 3y agoSo how would a statically typed language support conditionally adding methods at runtime? Lets say the code adds a method with name and parameters specified by user input at runtime. How could this possibly be checked at compile time?
- chowells 3y agoYou could add methods that nothing could call, sure. It would be like replacing the value with an instance of an anonymous subclass with additional methods. Not useful, but fully possible. Ok, it would be slightly useful if those methods were available to other things patched in at the same time. So yeah, exactly like introducing an anonymous subclass. But monkey-patching is also often used to alter behaviors of existing things, and that could be done without needing new types.
- bazoom42 3y agoHow would you emulate monkeypatching with anonymous subclasses?
- chowells 3y agoYou would need another feature in addition: the ability to change the runtime type tag of a value. Then monkey-patching would be changing the type of a value to a subclass that has overridden methods as you request. The subclasses could be named, but it wouldn't have much value. As you could repeatedly override methods on the same value, the names wouldn't be of much use, so you might as well make the subclass anonymous. In another dimension, you could use that feature in combination with something rather like Ruby's metaclasses to change definitions globally in a statically-typed language. I can't think of a language that works this way currently out there, but there's nothing impossible about the design. It's just that no one wants it.