lua-users home
lua-l archive

Re: Syntactic sugar for function calls

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


I'm doing something similar. Here's my current setup. I'm using 5.0beta.
areas = {}
function nominate(t) -- takes a numeric array, 
				-- and makes it an associative table
				-- through each member's "name" member
	table.foreachi(t, function(i, v)
		t[v.name] = v
		t[i] = nil
	end)
end
function area(area)
	nominate(area.rooms)
	if not area.name then
		error "Need to name this area"
	end
	areas[area.name] = area
end
function room(room)
	if not room or not room.name then
		error "Need to name this room"
	end
	return room
end
	
area {
	name = "house"
	rooms = {
		room { 
			name = "bedroom"
			desc = "Joe's Bedroom"
			foo = function()
				print("Hello!")
			end
		},
		room { 
			name = "bathroom"
			desc = "Wendy's bathroom"
		}
	}
}
print(areas["house"].rooms["bedroom"].desc)
print(areas["house"].rooms["bathroom"].desc)
areas["house"].rooms["bedroom"].foo()
Hope this helps.
Ben
On Sunday 23 February 2003 03:51 pm, Rob Kendrick wrote:
> Hi,
>
> I was playing around with calling functions without the brackets, if
> they take only one parameter, and it is a string or table this evening.
>
> function user(t)
> ...
> end
>
> user { name = "Joe Bloggs", password = "wibble" }
>
> Works, where adding another function, say:
>
> function hosts(t)
> ...
> end
>
> user { name = "Joe Bloggs", hosts { "jbloggs.bigco.com" } }
>
> Does not. Which seems a shame. Is it me just getting something rather
> stupidly wrong, or can I not do this? Is there an alternative method in
> the same vain? (this is for a configuration file.)
>
> Thanks for your time,

AltStyle によって変換されたページ (->オリジナル) /