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 Soni L. once stated:
> Hello!
> 
> Why does table.concat not respect __concat? I wanted to make it error 
> with a custom error message.

	mt = {}
	function mt.__concat(a,b)
	  local r = {}
	  for n,v in pairs(a) do r[n] = v end
	  for n,v in pairs(b) do r[n] = v end
	  return r
	end

	a = { one = "1" , two = "2" , three = "3" }
	b = { four = "4" , five = "5" , six = "6" }
	c = a .. b

	d = { "This is string 1" , "This is string 2" }
	e = table.concat(d,"\n")

  How do you propose the above code to work if table.concat() respected
__concat?

  Besides, you can provide your own error code for table.concat():

	local tc = table.concat
	table.concat = function(list,sep,i,j)
	  local okay,ret = pcall(tc,list,sep,i.j)
	  if not okay then ... end
	  return ret
	end

  -spc