Open nex3 opened 2 years ago
CC @rakudrama
Lets assume we make JavaScript symbols behave as a distinct Dart type that implements all the methods declared for Dart Object
. There is a problem of how we implement some of these methods.
The Dart maps use hashCode
, but there is no obvious way to give different JavaScript symbols different hashCode
values.
You can't store the hashCode on a property of a JavaScript symbol (it is not an object with properties, so JavaScript creates an Object wrapper, stores the property there, and then discards the temporary wrapper). You can't use a JavaScript symbol as a key to a JavaScript WeakMap. The implication therefore is that all JavaScript symbols have the same hashCode
value and the map lookup degenerates to a linear lookup.
Another approach would be to treat JavaScript symbols specially in Dart maps. Strings are already treated specially (they have similar problems with hashCode
). The downside of treating JavaScript symbols specially is that it would add another test on a hot path to handle an issue that is rare enough that has not been noticed until recently.
Even linear lookup would be preferable to having an object that simply crashes your application if you put it in a Map
. That said, why not set Symbol.protoype.get$hashCode
to a function that returns the hash code of Symbol.toString()
?
Passing an instance of JavaScript's
Symbol
class as a key to a DartMap
causes an error like the following: