lua-users home
lua-l archive

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


Thanks for the comments on this (even though I specifically asked for help
with implementation and not criticism 
of what I was trying to implement!)

Luiz Henrique's confirmation that my original intention was not going to be
easy to parse (lex?) got me thinking 
about alternatives.

Javier Guerra Giraldez's idea (which may have been rhetorical) that it
should be reduced right down to {cat, dog} 
won't do because it cannot be distinguished from the value-only syntax
shortcut for lists. We need some kind of 
marker to indicate that the name is supposed to be the key rather than a
variable specifying the value. A prefix 
character would be the shortest solution, and easier to parse. Having got to
that realisation, the obvious choice 
is a dot, echoing the dot syntax for table accesses. So:

t = {.cat, .dog} --> t = {['cat'] = true, ['dog'] = true} (or t = {cat =
true, dog = true}).
t = {[cat],[dog]} --> t = {[cat] = true, [dog] = true}

And, for regularity:

t = {.cat = true, .dog = true} -- Dot prefix should be allowed, but not
required, for a name = value declaration 
(any value).

Of course one can construct a set by constructing a list and then doing a
procedural fix-up, but that is quite 
inefficient, involving an unnecessary full table traversal and (probably)
reallocation of all the elements from 
the array-part to the hash-part of the table. It also means the keys have to
be quoted which clashes with the 
"user illusion" that they are names in expressions such as:

if t.cat then print("Cat is a member of the set") end

More importantly, it is unlikely to be stumbled upon by beginners who will
just live with (or grumble about) the
need to type all those "=true" values!  (And from a design point of view it
should be bad form to expose an 
implementation detail like this in a declaration).

Lua is billed as "offering an expressive syntax for creating tables" and "a
powerful general-purpose procedural
data-description language" (1), so the expressiveness and brevity of table
constructors is a valid discussion.

I will implement this in the Lua 5.3.1 version of my powerpatch so I and
anyone else interested can try it out.

(1) "The Evolution of Lua" by Roberto Ierusalimschy, Luiz Henrique de
Figueiredo and Waldemar Celes.



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus