lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Le Sun, 15 Nov 2009 11:59:20 +0200,
steve donovan <> s'exprima ainsi:

> The proposal which seems most Lua-like is to add a __methindex which
> when enabled will catch all method lookups. In its absence, then
> __index works for all lookups.


Probably it's a stupid question (let's be bold!): I cannot figure out how __methindex solves the issue. Namely, how can the language guess whether s[x] is supposed to access a method or plain "data"? How does the language guess the programmer's intent, and accordingly decides to use __methindex or not?

The way I see it is a general point with object types that are basically containers, like sets (or more commonly lists, mappings). There is a need to separate proper container data from "machinary" attributes under the hood. This is done for languages that have such types builtin, even if they have other transparent attributes (indeed, they have). As a consequence, for instance, iterating over a cointainer only shows the "real" data, as the user intends.

If we implement a container in lua as is, we cannot add custom "machinary" slots without having them visible, as opposed to builtin behaviour - or can we? In other words, if I'm right on this, the container is no more a "clean sheet" (an expression I steal from Steve Dekorte, the author of Io language).
Note that the issue is not strictly limited to methods (eg a list could hold metadata about its items), and can also appear for non-container types (which inherit custom "generic machinery" in addition to type-specific attributes). A solution I use is to have eg a "data" slot to which usual access (such as for iteration or membership test) is silently delegated. Non-data slots can only be accessed explicitely by name.

* la vita e estrany *