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 Dibyendu Majumdar once stated:
> 
> It is actually relatively easy to add a table subtype that has a
> defined 'length'. Not computed length but defined length. I already do
> this in Ravi. Lua could adopt this approach - allow via syntax or API
> call to mark a table as having a defined length. Such tables should
> not allow metamethod on #, and should only allow the table to grow by
> 1 when entry is added to [#+1]. This is all prior art in the sense
> that Ravi already does this. The only thing that I haven't been able
> to figure out yet if there is a nice syntax for 'constructors' that
> can enforce this - e.g. could one use []{ .... } or [ ... ]. The
> latter may have some ambiguities.
> 
> I haven't had the time but at some point I will extend the current
> Ravi array subtype to allow generic array subtypes - not just
> number/integer ones.

  In another message, I prososed the following:

	x = { "one" , "two" , nil , "four" , "five" }
	-- creates an array of defined length

	x = {}
	-- still an array of defined length (0)

	x[#x + 1] = "hello"
	-- adjusts the defined length by one

	table.insert(x,"there")
	-- also adjusts the defined length

	table.remove(x)
	-- adjusts the defined length by -1

	table.move(x,M,N,I)
	-- adjusts the defined length appropriately

	table.pack(...)
	-- returns an array with a defined length
	-- removes the "n" hack.

and while this should probably work to make an array of defined length:

	x = { [1] = 1 , [2] = 2 , [3] = nil , [4] = 4 , [5] = 5 }

this should probably *NOT* work as it would complicate the parser:

	x = { [4] = 4 , [1] = 1 , [3] = nil , [5] = 5 , [2] = 2 }

> In my opinion, Lua should either say there are no array semantics, it
> is all a map; or provide an array subtype, rather than doing this type
> of clever hack which is nonetheless a hack. For the sake of the poor
> user.

  +1.

  -spc