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 http://lua-users.org/lists/lua-l/2005-02/msg00666.html 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....

Doug

-- 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
     end
  end

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

     if f then
        return f
     end
  end

  return _G[funcname]
end   -- flookup()

function colonfunc(s)
  local r, n
  r, n = string.gsub(s,
                     '(%w*):(%w*)([(][)])',
                     '(flookup(%1, "%2"))(%1)')

  if n < 1 then
     r, n = string.gsub(s,
                        '(%w*):(%w*)([(])',
                        '(flookup(%1, "%2"))(%1, ')
  end

  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()'))
29
> print(colonize('s:gsub("%s",":")'))
Lua:is:colonizing:my:strings!   4
>

--
--__-__-____------_--_-_-_-___-___-____-_--_-___--____
Doug Rogers - ICI - V:703.893.2007x220 www.innocon.com
-_-_--_------____-_-_-___-_--___-_-___-_-_---_--_-__-_