13 May 2006

Healing the Object/Map Divide

An object (or, more precisely, its state) is just a map where the keys are strings from a certain grammar. Many languages allow attributes to be dynamically added and removed. Consider removing the restriction on the keys - allow any object, not just a string [1]. Then we don't need the object/map divide! [2]

Ideally a language should define the '.' operator in terms of []: obj.attrib = obj['attrib']. That is, every object is a map, and attribute access is just a special case of map lookup with a different syntax, where you specify the attribute name as an identifier rather than an arbitrary experssion that could be computed at runtime [3].

Yes, with this generalization, the '.' operator won't be able to access all attributes (but only what it can access otherwise - where the keys are strings that are valid identifiers). But this is a reasonable state of affairs.

This would be beautiful. We'd be getting rid of one whole language feature - attributes, as different from entries in a map.

[1] Python accepts only strings for the keys, but any string will do - not just valid identifiers.
[2] There may be a need for different implementations optimized for different cases, but we don't need different abstractions.
[3] Javascript has this equivalence.

Last Updated: 18 May 2006

No comments:

Post a Comment