lua-users home
lua-l archive

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


Olivier Delannoy wrote:

Why : is more complex than it appears ?
a.truc(a, something) is strictly equivalent to a:truc(something)

Strictly speaking, a:truc(something) is equivalent to (in
pseudo-code since you can't write this in Lua):

  { local <gensym:1> = a; <gensym:1>["truc"](<gensym:1>, something) }

To see the difference, try this:

do local registry, i = {}, 0
  setfenv(0, setmetatable(getfenv(), {
    __index = function(self, key)
      local i = (registry[key] or 0) + 1
      registry[key] = i
      return {
        n = i,
        truc = function(self, ...)
          print(("%q %d truc%s")
                   :format(key, i, i == 1 and "" or "s"),
                ("%q %d truc%s")
                   :format(key, self.n, self.n == 1 and "" or "s"),
                ...
               )
        end
      }
    end
  }))
end

a.truc(a, "hello"); a.truc(a, "world")
b:truc"hello"; b:truc"world"