lua-users home
lua-l archive

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


From: Andrew Gierth <andrew@tao11.riddles.org.uk>
Subject: Re: Fwd: Re: new thought experiment: what would you add to
	Lua ?
To: Richard Green <pslmsngr4lua@gmail.com>
Cc: lua-l@lists.lua.org
Message-ID: <87d0telr22.fsf@news-spur.riddles.org.uk>
Content-Type: text/plain; charset=utf-8

"Richard" == Richard Green <pslmsngr4lua@gmail.com> writes:

Richard> 2) Sets -- In page 15 of /Programming in Lua, third edition/
Richard> you state, "We use tables to represent ordinary arrays,
Richard> *sets*, records, and ...".  The one thing that I miss from
Richard> Pascal in modern programming languages is Pascal's set,
Richard> especially inside an if statement.

A "set" in Lua is simply a table where the keys are the members of the
set and the values are not significant (any true value will do).

current_month = 'Sep'
summer_months = { Jun = true, Jul = true, Aug = true }
if summer_months[current_month] then
 -- stuff
end

--
Andrew.

A neat addition to support this (again I have a Powerpatch on the Lua User WiKi) is to make value default to boolean true in table constructors. Then you can write:

summer_months = {['Jun']; ['Jul']; ['Aug']}

Due to the need to distinguish keys from values, this does not save much typing, but it does give 'parity of esteem' to sets with the existing implementation of lists and maybe makes the intent a bit clearer.