lua-users home
lua-l archive

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


But there remains the common cases of indexes by numbers, which still require the [] in table constructors.
Not all tables are constructed as sequences, writing t = {1, 2, 3, [10] = 100,} or t = {'a', 'b', 'c', [25] = 'y', 'z', [-1] = '',} is ugly
It could simply be  t = {1, 2, 3, 10 = 100,}  or t = {'a', 'b', 'c', 25 = 'y', 'z', -1 = '',}
Such use is not exceptional, it exists with sparse-indexed lookup tables (which are frequent in many projects). These brackets obscure the data making them unnecessarily long if this is a large table constructor with many keys.

As well the way the tables are dumped in debugging consoles is ugly and verbose, all key strings are surrounded by quotes and brackets, when they don't need to, if strings are valid identifiers (not the very few reserved keywords). And the same happens with number keys surrounded by brackets (for all keys, even those in a sequence: see how "mw.logObject(t)" logs tables and subtables in Wikimedia, with one item per line all lines indented, this generates lot of pages to scroll to find anything; this is not a bug of Lua but a bad design of the mw package for this function, which uses considerable code for indenting and finding subtables references and avoid infinite cycles, but not to make it really readable: the indentation offers little interest on very tall and very narrow lists when sequences should not even need any line break and were most numeric keys are not needed and can be abbreviated).



Le lun. 25 mai 2020 à 10:57, Francisco Olarte <folarte@peoplecall.com> a écrit :
Andrea:

On Mon, May 25, 2020 at 2:33 AM Andrea <andrea.l.vitali@gmail.com> wrote:
> Overall the table constructor is not bad.
> One source of mistake for me is that when I see a name=value in the table constructor, the name looks like a variable to me, not a literal string, because any hint is missing: no quotes, no dot

On many scripting languages, and I suspect lua too, this in intentional.

Let me explain how it works for me. When I use tables  as
struct/objects they read naturally:

local point = { x=1, y=2, z=3}

An when I use them as named parameters they do too:

return outgoing_trunk:make_call{caller=src_number, called=dst_number,
time_limit=120000 }

And it couples naturally with the shortcut indexing access point.x

I do not normally have non-keywords elements in constant tables.

Normally in all my real world access to any-string, or number keys I
do indexing and I have the data in some var.

The exception being some test structs:

local test_routes = { ["34912345678"] = spain_trunk }

But in production I normally would have read that from some data
source an do test_routes[number]=route.

FOS