lua-users home
lua-l archive

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


A radical idea I once had to solve the table appending problem and other
similar things...

Give the language a unary PLUS operator and have two different numeric
types called Number and PlusNumber which have exactly the same
implementation. A PlusNumber is simply an ordinary Number which has had
unary plus applied to it, and in fact this is the only way to create a
PlusNumber. The two numeric types can be distinguished by the type()
function, but there is no way to tell which is which from their behaviour.

For all arithmetic purposes, Numbers and PlusNumbers are interoperable and
work exactly the same (indeed they are implemented internally by the same
C code). When arithmetic is performed between them, the result is always
an ordinary Number. However, in certain circumstances, the difference is
useful. When used as a table index, a PlusNumber could imply an offset
from the last element of the table, so if you wrote

	t[+1] = "New last element"

...it would add an item to the next free numeric index. I think this idiom
looks right, considering what it does.

The same principle applies to file offsets (ie, you could do without the
"whence" parameter if you used a PlusNumber to mean "offset from the
current file position"). In fact you can use it for any kind of offset -
bear in mind that you could write +(-5) to get a negative PlusNumber.

Perhaps this is a bit too crazy to be considered seriously but I thought
I'd suggest it anyway!

&.


#####################################################################################
This e-mail message has been scanned for Viruses and Content and cleared 
by MailMarshal.
The Blackpool Sixth Form College.
#####################################################################################