lua-users home
lua-l archive

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

One of Lua's greatest assets is the simplicity of the core. No Arrays and Hashes and Queues, we have simply Tables.

The merging of all of these concepts into one atomic type, however, has the collision of embedded nils - tables-as-hashes need a way to remove a key entirely; tables-as-arrays need a way to store an 'empty' value.

For the purposes of this discussion, let's assume that it makes sense to have both nil (nothingness, no value) and false (a value indicating non-truth) in the Lua language. We could go one step farther, like JavaScript, and add a third, similar value: 'undefined'. (That's what it's called in JavaScript land; we could call it whatever we want.)

In JavaScript, this:
	function foo( a, b ){
	   alert( a + ':' + b )
	foo( null )

In JavaScript this:
	var myHash = { foo:null }
	alert( + ':' + )

And to remove a key from a hash, you use the delete operator:

In JavaScript, null is not equal to false, but it IS equal to undefined. They are two shades of a very similar concept.

My proposal is to use a concept similar to JavaScript (3 'dead' values) but in a slightly different way.
	* false is a value that means "I am not true!"
* undefined (or other name) is a 'non-value' that means "This key exists, but has no useful value!" * nil is the current non-value that means 'emptiness', and removes any keys that have it as a value.

Calling a vararg function would transfer nil values into undefined values in the table:
	function foo( ... )
	   for i,v in ipairs( {...} ) do print( i,v,v==nil ) end
	foo( nil, 2, nil )
	--> 1	undefined	true
	--> 2	2	false
	--> 3	undefined	true

I'm sure my idea is not without it's flaws, but hopefully there is some merit in it. In my humble, Lua-newb opinion, the embedded-nil topic has been brought up enough to warrant (yet another?) attempt to fix it. I understand the clean design that brought Lua to its current point, but it _seems_ to me that people need a true solution.

I think the solution has to be:
	a) something like the above, allowing embedded nils; or
b) internal tracking of the true end of an array with array-specific iterators for tables; or
	c) a whole new array type of object that sits alongside tables

The 'solution' cannot be "track the internal size yourself and iterate it yourself" specifically because it is impossible in 5.1 to pass nil values to vararg functions and know that the user explicitly passed an empty value.