Purpose: to describe OO techniques and ways to implement things like message dispatch, inheritance, encapsulation.


JCW: A simple example of vtables (as C++ calls 'em):

  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: But the index tag is not available for strings, so the following fails:

  local String = {
    len = function (self)
      return strlen(self)
    end,
  }

  a="abc"
  print(strlen(a))

  settagmethod(tag(""),"index",
    function (x,i) return %String[i] end)

  print(a:len())


Last modified
2001-02-01

(216.232.137.139)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated