lua-users home
lua-l archive

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

Gunnar Zötl wrote:

As strings and numbers are immutable values, it would
not make much sense to directly attach a metatable to such a value.

[I do not follow the reasoning here - why immutability plays a role in whether or not a metatable makes sense. But that's really not at issue to what I've proposed.]

... But then the lookup does not take place in the context of your value any more. ... Thus, you'd end up with two meanings of the colon operator, that differ in both semantics and binding.

Thanks for the comments, Gunnar. Indeed, thanks to all who have commented on this thread.

I wrote a longer version of this, but I've decided to state it briefly (it's long enough):

(1) My original proposal is more complex than necessary.

(2) My simpler proposal is slightly more than Adrian Sietsma indicated:

 x:func(...) ==> (x.func or _G[type(x)].func or func)(x, ...)

(3) This does not require metatables for the other types.

(4) This *does* require a core change (as in Josh Jensen's LuaPlus).

(5) Some of the global tables needed for (2) already exist in the standard.

(6) Where to place inconsistencies and incompleteness is a matter of taste, and I find it more tasty to have the colon operator applicable to all data types, rather than scoped to the context of the variable and limited in applicability.

(7) I'm after improvements in the total Lua experience, not just The Core.

(8) If the meaning were originally as above, I seriously doubt that many would be arguing to reduce its scope, though they may be arguing for a better implementation (__methods, non-table metatables, etc.).

(9) Clearly there are enough detractors that this won't happen anytime soon. Or, the detractors are of enough stature that it would be silly to argue with them (and I do not mean that in a bad way).

So for now such conveniences as s:len() will be for me and LuaPlus users. Now I've just got to port LuaPlus to Unixy OSes....


-- colon.lua
-- Colonize your own strings!
-- No Core changes necessary!

function flookup(object, funcname)
  local f

  if (type(object) == 'table') or (type(object) == 'userdata') then
     f = object[funcname]

     if f then
        return f

  if _G[type(object)] then
     f = _G[type(object)][funcname]

     if f then
        return f

  return _G[funcname]
end   -- flookup()

function colonfunc(s)
  local r, n
  r, n = string.gsub(s,
                     '(flookup(%1, "%2"))(%1)')

  if n < 1 then
     r, n = string.gsub(s,
                        '(flookup(%1, "%2"))(%1, ')

  return loadstring('return ' .. r)
end   -- colonfunc()

function colonize(s)
  local f = assert(colonfunc(s))
  return f()
end   -- colonize()

$ lua
Lua 5.0.2  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> dofile'colon.lua'
> s='Lua is colonizing my strings!'
> colonize('s:print()')
Lua is colonizing my strings!
> print(colonize('s:len()'))
> print(colonize('s:gsub("%s",":")'))
Lua:is:colonizing:my:strings!   4

Doug Rogers - ICI - V:703.893.2007x220