lua-users home
lua-l archive

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


On 04/11/2014 07:59 AM, Sean Conner wrote:
It was thus said that the Great Thomas Jericke once stated:

I wrote:
a,b,c in table -> table["a", "b", "c"]

If you want to expand left to right you get

a,b,c in foo = (x,y,z in bar) in baz

foo["a", "b", "c"] = (bar["x, y, z"]) in bat -- syntax error: name
expected near "(bar["x, y, z"])"

The statements in front of the in must be names not expressions, if you
start to mix them the code will get ambiguous. Example:

local t = {}
local table = { t = "lala" }
table[t] = "dada"

local test = t in table
print (test) -- lala or dada ?
   No ambiguity here.

	local t = {} -- table: 0x896b2b8
	local table = { t = "lala" }
	table[t] = "dada" -- LINE 3
No there is none, the ambiguity is on the line with the question mark.

   You now have
table[ table: 0x896b2b8 ] = "dada"
   	table[ 't' ] = "lala"
because at line 3, you used the table t as the index, not the string "t",
and in lua, tables are valid keys.  As I understand you,

	x in y

resolves to

	y["x"]

that is, x is used as a string, "x".

   What you failed to mention (or I did not pick up on) is that the syntax

	x in y

is NOT an expression,
"x in y"IS an expression, but the "x" isn't, it can't be a name or an expression it needs to be either.
The syntax is:
    MyNewInExpression ::= namelist 'in' exp

You may get the definition of namelist and exp from http://www.lua.org/manual/5.2/manual.html#9
even though it looks like one, because you can do:

	test = t in table -- how is this not an expression?

which is

	test = table['t']

which is "dada".  So, given what you said, I would assume the following is a
syntax error:

	function blah()
	  return "one","two","three"
	end

	foo = { "one" = 1 , "two" = 2 , "three" = 3 }

	a,b,c = blah() in foo

because of the same reason that

	a = foo[blah()]

works but

	a = foo.blah()   -- or
	a = foo.(blah())

fails.
Exactly, if you try to write (a, b, c in table1) in table2 you try to do the same as if you write:
table2.(table1.a), table2.(table1.b), table2.(table1.c)

This is not possible.
--
Thomas