lua-users home
lua-l archive

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


Am 29.01.2018 um 12:02 schröbte steve donovan:
On Mon, Jan 29, 2018 at 12:32 PM, Grey Knight
<tinyplasticgreyknight@yahoo.com> wrote:
wouldn't be sure how to make it work when e.g. I have some table which I'm
treating as an object of a specific type.  Maybe a __type metafield can
give a string name for its type.

It's a surprisingly tough problem. Because we use these implicit,
ad-hoc types - how to specify them so we don't lose all that wonderful
flexibility? I believe the TypedLua research project has some
interesting things to say about possible solutions.  That's why I said
that Lua and Javascript tend to think alike, at least js before the
demands for class _syntax_ got overwhelming. In the case of Lua, there
is little agreement on how to _define_ 'classes', so some extra
annotation is needed - this is a `Cat`.

Personally, I like Dirk's "has" function[1]. I have reimplemented it in C, added some memoization, and allowed specifying metamethods in addition to plain field names, so that `assert(has"__call"(arg1))` would check for a function or anything with a `__call` metamethod. It leads to minimal interfaces, and it can handle most OO modules (those that use some form of `__index`ing), including sub-typing and userdata objects.

Philipp

  [1]: http://lua-users.org/lists/lua-l/2013-05/msg00426.html