4 ms·
> 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.
by 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.
- bazoom42 3y agoSo how would you compile code which contains a call to a method which is only defined at runtime?
- chowells 3y agoIn a dynamic language, everything is only defined at runtime. Given that, a sketch of a statically-typed system would be something like... At the time a definition is added to the environment, you type check against known definitions. Future code can change implementations, as long as types remain compatible. (Probably invariantly, unless you want to include covariant/invariant annotations in your type system...) This doesn't change that much about a correct program in a dynamic language, except that it may provide some additional ordering requirements in code execution - all the various method definitions must be loaded before code using them is loaded. That's a bit more strict than the current requirement they the methods must be loaded before code using them is run. But the difference would be pretty tractable to code around. And in exchange, you'd get immediate feedback on typos. Or even more complex cases, like failing to generate some method you had expected to create dynamically. Ok, I can actually see some appeal here, though it's got nothing to do with monkey-patching.