lua-users home
lua-l archive

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



Tuomo Valkonen wrote:
Personally,
I'd prefer something like Ruby's @foo as syntactical sugar for self.foo,

For those who like sugar, here is a version of lauxlib.c that changes luaL_loadfile and luaL_loadbuffer to search and repace @somename for self.somename and @somemethod(...) for self:somemethod(...)
You can also use # for comments.

http://lua-users.org/files/wiki_insecure/users/pshook/lauxlib.c

- Peter Shook

$ cat eg.lua

List = setmetatable({}, {__index = table})
List.__index = List

function List:new(...) return setmetatable(arg, self) end
function List:push(x) @insert(x) return self end
function List:pop() return @remove() end
function List:shift() return @remove(1) end
function List:append(list)
 for _,elem in ipairs(list) do @insert(elem) end
 return self
end

function List:fun(a, b)
 @a = a
 @b = b
 return @n
end

x = List:new(1, 2, 5)
x:foreachi(print)
x:push(99)
print(x:concat', ')
print('fun', x:fun('one', 'two'), x:shift())
x:foreach(print)

$ lua eg.lua
1       1
2       2
3       5
1, 2, 5, 99
fun     4       1
1       2
2       5
3       99
a       one
b       two
n       3

_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail