lua-users home
lua-l archive

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

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

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.

- 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

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

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

$ 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*