lua-users home
lua-l archive

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


On 07/12/2012 04:57, Andrew Starks wrote:
Something that u said was different than my understanding, which is
that pairs was a magic function, not implement-able within lua. I
believe that is true of "next", but pairs more-or-less just wraps
that. I don't think pairs is magic, am I wrong?

The magic actually is there, in function next. I guess the base of the proposal (and of this exchange) is whether it would be a good thing to bring it one semantic level higher, into the notion of traversal; to have a traversal syntax actually hiding implementation issues (an iterator or generator func, however it is implemented) and just propose the meaning to users.

I guess we need both, and maybe python does things right, there. If ever the thing denoted in syntax is a table with a dedicated metafield, then use it to produce the generator, else the thingie there should be a generator itself. Which gives (intentionally using very different syntax)
	across col with item
	across gen with item
	across genfact() with item
(where 'item' can actually be var tuples as in 'key, val' or 'idx, elem' or such)

Probably it's not that hard to switch from one semantics (col traversal) to the other (getting items from a generator) if the issue is not frontally obscured by implementation weirdities: after all, where are the supposed to come from? They must be handed to the traversal cycle somehow... (so, let us just try to express that in code: this necessarily leads to a kind of tuple with an actual item-generating func on one hand and some information about where_we_are_now on the other: no black magic there, no very hard difficulty neither)

Denis