lua-users home
lua-l archive

Re: Why do we have ipairs?

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


It was thus said that the Great William Ahern once stated:
> 
> What about label references? That is, if Lua allowed directly taking a
> reference to a label? Then you could use them directly, as when writing a
> classic state machine, or build your own jump tables which mapped keys to
> labels. Although you might have to add an optimization to memoize constant
> tables in the latter case.
> 
> The label reference would have to be a new native type. But it might be a
> little more elegant at the syntax level.
> 
> State machine example
 For state machines, you can use functions, because Lua supports tail call
optimizations, which are essentially gotos. I implemented TFTP using that
method:
	local read_data
	local send_data
	local receive_ack
	local receive_data
	local write_data
	local send_ack
	read_data = function() return send_data() end -- [1]
	send_data = function() return receive_ack() end
	receive_ack = function() return read_data() end
	receive_data = fucntion() return write_data() end
	write_data = function() return send_ack() end
	send_ack = function() return receive_data() end
 Both the client and the server can use the same state machine---where they
start depends on who is sending the data, and who is receiving the data.
 -spc (I like this much better than using a switch or goto statements)
[1]	Obviously this is just a sample of the code. There's a bit more
	to it, like error checking, resending, etc. 

AltStyle によって変換されたページ (->オリジナル) /