jason.orendorff at gmail.com
Wed Nov 14 11:49:41 PST 2012
On Fri, Nov 2, 2012 at 4:30 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote:
> There is no verifiable formal contract. But there can be an informal
> contract. In my experience, it is very important when using a dynamic
> language to recognize and try to support such informal contracts.
Well, I sort of agree. But even Ruby, as much as it owes to Smalltalk,
provides same-named methods in Array and Hash with completely incompatible
semantics. Hash»delete takes a key argument; Array»delete takes a value.
Array»each yields values; Hash»each yields key-value pairs. I expect this
is just because the operations you need from an Array vs. a Hash are really
different in practice. Their abstract conceptual similarity doesn't seem to
make the jump to the real world.
Returning to JS: is there really an informal contract that unifies Array,
Map, and strings? In the current spec, there's not a single method that
works the same way across all three!
The only thing that even *exists* across all three is .@@iterator(), which
is no small thing. But note that Array.prototype.@@iterator yields element
values, String.prototype.@@iterator yields UTF-16-encoded characters, and
Map.prototype.@@iterator yields key-value pairs.
A common protocol could certainly be added. For the sake of concreteness:
they could all have .hasKey(k), .hasValue(v), .get(k), .size, .keys(),
.values(), .items(), .forEach(f); and on mutable collections, .set(k, v),
.delete(k), .clear(). But I think the aesthetic appeal of this sort of
thing is way out of proportion to its practical value.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss