[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Syntactic sugar for sets
- From: "John Hind" <john.hind@...>
- Date: Sun, 24 Feb 2008 12:24:26 -0000
About a year ago, Peter Jacobi suggested a syntactic sugar for sets:
planet = {["mercury"], ["venus"], ["earth"]} -- In Lua 5.1, Gives Error '='
expected
Would be compiled as:
planet = {["mercury"]=true, ["venus"]=true, ["earth"]=true}
http://lua-users.org/lists/lua-l/2007-03/msg00767.html
Of course, Peter's syntax could be further simplified to:
planet = {[mercury], [venus], [earth]} -- In Lua 5.1, Gives Error '='
expected
Then you could write:
local input = "pluto"
if planet[input] then
print(input, " is a planet")
else
print(input, " is not a planet")
end
I think this is a really nice idiom and entirely within the spirit of Lua.
It expresses sets using the standard table mechanism + syntax sugar in
exactly the same spirit that arrays and objects are implemented in Lua.
Of course, responses focussed on pointing out that you can already do this
and various ways of expressing it within the existing language. But that's
not the point. Having it in the basic language definition makes Lua better
and more expressive at negligible cost. It also allows mixed idioms in data
description that are difficult to implement generically using factory
functions:
local mark = {name="Mark Jones", age=34, [male]}
I definitely vote this excellent suggestion be seriously considered for a
future version of Lua!
- John Hind