5 ms·
> Basically, I don't think there is too much chance to find a valid use for this behavior, but it is very hard to prevent it without complicating the language s
by twic 6y ago
> Basically, I don't think there is too much chance to find a valid use for this behavior, but it is very hard to prevent it without complicating the language significantly.
One approach is to provide a mechanism for cleanup that doesn't require the object to still be alive. Java has phantom references for this:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ref/PhantomReference.html https://docs.oracle.com/en/java/javase/11/docs/api/java.base...
https://stackoverflow.com/a/43659951/116639 https://stackoverflow.com/a/43659951/116639
And i learn that Smalltalk introduced the "ephemeron", which is now present in some other languages:
https://en.wikipedia.org/wiki/Ephemeron https://en.wikipedia.org/wiki/Ephemeron
- tsimionescu 6y agoSure, there are other possible cleanup mechanisms, and I believe Java is depreciating finalizers in favor of those other mechanisms. My point was only about finalizers themselves: a language with the finalizer mechanism I was describing above would need to add a lot of features to prevent finalized objects from becoming reachable again.
- masklinn 6y ago> One approach is to provide a mechanism for cleanup that doesn't require the object to still be alive. Java has phantom references for this: Java has pretty much deprecated finalizers due to how non-deterministic their run is. Modern java uses try-with-resources blocks which… are the exact same thing as Python's with.