lua-users home
lua-l archive

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

The Lua spec uses an imperative form which is unnecessarily verbose (it requires rewriting the table name and all indexing keys for tables and subtables. This syntax in the Lua specs does not even allow using the generated "text" with local semantics to create larger structures containing or referencing the same data.
What I suggest is something that can allow safe data-only structures (including any graph) to be serialized completely It just uses the powerful concept of closures to apply scoping rules to the existing "{}" notation, then adds a way to mark some keys or values in tables (because both can create circular links) with a reference than can be used anywhere in the data structure to reconstruct it exactly, and even embed the structure into another one without scoping problems.
Theses "variables" are even more powerful than id="" attributes in HTML (which are in a single namespace): you don't need any complex renaming scheme. Everything is already in Lua: while parsing the "{}" not only a new table is being generated, but another table is used to contain the variables in the same scope, this internal table of variables, created when you encounter the leading "{' is discarded/garbage collected when you reach the closing "}"; this internal table of variable also keeps the track (for example in its associated metatable) of the scopes of new subtables created inside.
The implementation of this is easy to do in Lua itself, but it could as well be part of its native syntax. It could also also facilititate debugging (e.g. print(table1) can dump all its content without just printing "table" and without falling into an infinite loop of self-recursions, using the same tracking technic used in the Lua specs that unfortunately generates a lengthy multiline output with many assignments and lot of redundancy)

Le dim. 4 nov. 2018 à 22:07, Coda Highland <> a écrit :

On Sun, Nov 4, 2018, 1:28 PM Dirk Laurie < wrote:
> {                                           -- starts a new local context (closure)
>  [:      ref0 = 'k1'] = :      ref1 = v1,   -- ['k1'] = v1   and set external ref0 = 'k1'
>  [:      ref2]        =               v2,   -- [1]    = v2   and set local    ref2 = 1
>  [:local ref3]        =               v3,   -- [2]    = v3   and set local    ref3 = 2
>  [:local ref4 = 'k2'] = :local ref5 = v4,   -- ['k2'] = v4   and set local    ref4 = 'k2', local    ref5 = v4
>  [:local ref6 = 'k3'] = :      ref7 = v5,   -- ['k3'] = v5   and set local    ref6 = 'k2', external ref7 = v5
>                  k4   =               ref0, -- ['k4'] = 'k1'
>  [              'k5'] =               ref2, -- ['k5'] = v2
>                                       ref1, -- [3]    = v1
> }                                           -- close the local context

Why is this better than writing it out in plain Lua that everyome can
already understand?

I understand the intent is that it can express internal references (possibly circular) in a single literal instead of having to write the plain object and then imperatively link in the references using subsequent statements.

This could be implemented by a preprocessor that translates it into an IIFE that does exactly that.

/s/ Adam