lua-users home
lua-l archive

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

About a year ago, Peter Jacobi suggested a syntactic sugar for sets:

planet = {["mercury"], ["venus"], ["earth"]} -- In Lua 5.1, Gives Error '='

Would be compiled as:

planet = {["mercury"]=true, ["venus"]=true, ["earth"]=true}

Of course, Peter's syntax could be further simplified to:

planet = {[mercury], [venus], [earth]} -- In Lua 5.1, Gives Error '='

Then you could write:

local input = "pluto"

if planet[input] then
	print(input, " is a planet")
	print(input, " is not a planet")

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

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