lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark Hamburg wrote:
[...]
> Is there some way to extend the table construction syntax so that one could
> write function calls within the table constructor which would receive the
> table as a parameter?

IIRC, the way Lua constructs tables internally is to push all the initialiser
elements onto the stack, and then construct the table with a single VM
opcode... which means that while executing the initialisers, then there is no
table yet. Which means that I don't think this is possible as a core function.

*However*, it ought to be quite straightforward to use something like metalua
or a token filter to rewrite this:

>     doWithTable{
> 
>         field1 = "foo",
> 
>         addLotsOfFields( $, 1, 2, 3 ),
>         addMoreFields( $, "a", "b" )
> 
>     }

...into this:

	doWithTable (function() local t = {
			field1 = "foo"
		}
		addLotsOfFields(t, 1, 2, 3)
		addMoreField(t, "a", "b")
		return t end)

(The fiddling with function() end is to emulate a block expression.)

Likewise, this

>     t.foo?.baz

...could be done in exactly the same way, being rewritten to, most likely,
your kEmptyTable alternative as the simplest and fastest.

In general, anything that smacks of syntactic sugar is best done in an add-on
layer rather than in the language core!
	
- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGyxA6f9E0noFvlzgRAlbgAKCHHiOo0LnsT4IK82CW1EKCyp9HIgCdGgQD
sI9AFtPLf9v5Yn6lfWTNGHA=
=shyI
-----END PGP SIGNATURE-----