Open DartBot opened 11 years ago
This comment was originally written by @seaneagan
I think that's what Expando is for:
http://api.dartlang.org/docs/continuous/dart_core/Expando.html
but I could be wrong. I assume it will be implemented with JS WeakMap or a polyfill thereof where necessary:
Removed Type-Defect label. Added Type-Enhancement, Area-Library, Triaged labels.
This comment was originally written by @seaneagan
AFAIK, Expando in dart2js is not yet weak. Here is a polyfill for JS WeakMaps which could be used to fix this:
https://code.google.com/p/es-lab/source/browse/trunk/src/ses/WeakMap.js
Expando in dart2js is "weak" in the sense that adding an expando property to an object (the key) will not keep the key object alive on its own. The key will keep the value (whatever the expando property refers to) alive.
Without having spent too much time looking at it, I think we're basically doing the same thing as the polyfill you refer to (we store an extra hidden property on the key).
This comment was originally written by @seaneagan
Cool. According to the polyfill docs, this approach still can have leaks (but maybe dart2js avoids this?), so using JS WeakMap when available could still be advantageous.
I guess the leak is that if the Expando object dies, the properties on the objects will not be removed, which will keep the value alive. This is where it fails to implement an ephemeron table or a proper weak map.
Is expando in dart2js weak these days?
cc @floitschG.
As weak as ever. Nothing changed since Kasper's response in May 2013.
Current browser support for WeakMap: http://kangax.github.io/compat-table/es6/#WeakMap
Although IE11 has only 2/5, it looks like the 3 points are not that important (at least after a fast glance). Once we drop support for IE10 we should look at weakmaps.
Almost a year, time for a ping.
Weak references in the dart vm for Flutter would be useful.
A "weak reference" is very different from a weak map. The weak map doesn't expose garbage collection at all, it just optimizes around it (not keeping entries that you can't access any more). A weak reference actually changes behavior depending on garbage collection. You could say that the weak reference breaks the abstraction that garbage collection may or may not happen behind the scenes.
That said, this issue is a request for a weak map, and Expando
is the answer to that request. When compiled to JS, it is backed by a WeakMap
if that is available (and in most modern browsers, it is available, at least with the basic features needed here).
Just for reference, I've publish this package: https://pub.dev/packages/weak_map
@marcglasberg, now that Dart is supporting WeakRef and Finalizer, it would be great to release a new version of the weak_map package that uses WeakRef and Finalizer to support iterable keys and values. https://github.com/dart-lang/sdk/issues/47772
Fyi @polina-c who may be interested in helping implement this.
@jacob314 here is my implementation of WeakHashMap that use WeakReference and Finalizer. https://pub.dev/packages/weak
This issue was originally filed by @pjako
I would like to have weak references in dart, I know its hard to do in javascript, currently at least.
Currently I want to give an object some uniqueID and track it in a Map without preventing the object to get garbage collected.