lua-users home
lua-l archive

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


It was thus said that the Great Thomas Jericke once stated:
> On 04/10/2014 10:04 PM, Sean Conner wrote:
> >It was thus said that the Great Thomas Jericke once stated:
> >>
> >>-----Original Message-----
> >>>From: "Sean Conner" <sean@conman.org>
> >>>   How about:
> >>>
> >>>      foo = { a = 1 , b = 2 , c = 3 }
> >>>      bar = { x = 'one' , y = 'two' , z = 'three' }
> >>>      baz = { one = 'alpha' , two = 'beta' , three = 'gamma' }
> >>>
> >>>      a,b,c in foo = x,y,z in bar in baz
> >>Depends on the order of the in operarator, IMO it should be right to left:
> >>
> >>foo["a", "b", "x"] = baz["bar"]["x", "y", "z"]
> >>
> >>As  baz["bar"] is nil, you will get:
> >>
> >>attempt to index field bar (a nil value)I think what you wanted to write 
> >>is:
> >>
> >>a,b,c in foo =  baz[x,y,z in bar]
> >   Interesting.  Because in my mind, I was parsing it as
> >
> >	a,b,c in foo = (x,y,z in bar) in baz
> >
> >   (left to right) That is:
> >
> >	foo.a = baz[bar.x]
> >	foo.b = baz[bar.y]
> >	foo.c = baz[bar.z]
> >
> >   I'm not saying I'm right or you are wrong, just how I would interpet it.
>
> You have a mistake in you inner interpreter right there

  That's why I'm asking these questions, to clarify the concept.
  
> 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

  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, 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.

  -spc (And remember, you have to explain all this to someone learning Lua)