lua-users home
lua-l archive

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


Reuben Thomas <rrt1001@cam.ac.uk> wrote:

>The first rule of Lua: Do it in Lua.

Sounds like good advice for other scripting languages too.

>One of the best things about Lua is precisely that most of the things you
>might want to do, including changes to the language, can be done directly in
>the language itself.

Let's take Edgar Toernig's note in http://froese.exit.mytoday.de/sol/
Diffs2Lua :

    Tags are completely gone.  Instead, each object
    has an attached method table.

    [...]

    Advantages: 1) No special interface needed for tag methods.
    2) Object data and its methods are seperated.  No need to
    copy all methods into each object.  3) Every datatype has
    methods.  They mustn't be emulated with set/gettable.

Wouldn't the following standard Lua code solve this?

  local vtable = {
    _tag_ = newtag(),
    times = function (self,i) return self.v * i end,
    hello = function () return "hi!" end,
  }

  settagmethod(vtable._tag_, "index",
    function (x,i) return %vtable[i] end)

  t = {}
  settag(t, vtable._tag_)

  t.v = 111

  assert(t.v == 111)
  assert(t.none == nil)
  assert(t:times(222) == 24642)
  assert(t.hello() == "hi!")

-jcw