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 Rodrigo Azevedo once stated:
> I lost something here? The next phrase after my quote (not quoted here)
> says that this syntax is not good! (for pure tables)

  Okay, so a table without a metatable will raise an error if you do

	t1[a,b,c] = 1,2,3

but a table *with* a metatable *and* a __newindex() method will not.  I
suppose that means that a table *with* a metatable that does *not* have
__newindex() will still error out.  Okay, quick, which line will error out?

	foo[a,b,c] = 1,2,3
	bar[a,b,c] = 1,2,3

  To be safe, you would have to do:

	function getmetafield(t,field)
	  local mt = getmetatable(t)
	  if mt then
	    return mt[field]
	  end
	end

	if getmetafield(foo,"__newindex") then foo[a,b,c] = 1,2,3 end
	if getmetafield(bar,"__newindex") then bar[a,b,c] = 1,2,3 end

  Or you just do the former, let it error out, then swear as you track down
where bar was defined so you can add the appropriate metatable.

> I would like to stress that the proposal is not about multiple assigments
> or multidimensinal arrays. It is about to easily ( and fast, due to the
> extended use of the stack) implemention of these alternatives, as well as
> many more general cases.

  But the problem here is that in one case,

	foo[a,b,c] = 1,2,3

will generate an error, and in another case, 

	foo[a,b,c] = 1,2,3

will not.  One of the nice qualities of Lua (and one of the more annoying
qualities of Lua, depending on my mood) is the lack of typechecking.  Sure,
when I do this:

	sock = net.socket(address.family,'tcp')
	sock.reuseaddr = true
	sock:bind(address)

sock is of type 'userdata'.  But I can easily "fake" a socket:

	sock = {}
	sock.reuseaddr = false
	function sock:bind(address) ... end

and now it's a table.  The rest of my code doesn't care *how* it's defined,
as long as it *acts* like I expect it to act.  Sure, I can make sure my
mocked sock has a __newindex() method, but without your proposal, I don't
have to bother with defining that.

  Thus, if this:

	foo[a,b,c] = 1,2,3

fails for non-metatable tables, then I would reject the proposal because it
breaks expected behavior.

  -spc