lua-users home
lua-l archive

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


I also agree: #t is costly on sequences as they need first to be fully enumerated (and some sequences created from an input stream cannot be enumerated several times, it would cost too much to retain the full sequence in a cache for allowing it to be enumerated again for the for loop).
For tables and strings however, #t is instantaneous and ipairs() not needed... except when tables are not sequences and have integer keys outside the consecutive range 1 to N, that ipairs() could still enumerate all (ipairs can simply do the same as pairs(), filtering just keys according to their type, assuming that pairs() return all integer keys in ascending order.



Le jeu. 21 mai 2020 à 19:26, Andrew Gierth <andrew@tao11.riddles.org.uk> a écrit :
>>>>> "Coda" == Coda Highland <chighland@gmail.com> writes:

 >> why the _ipairs metamethod was deprecated?

 Coda> Because there's no need for a metamethod for something as simple
 Coda> as ipairs()

Some of us greatly disagree with this position - in particular, it
seriously impacts what you can do with userdatas.

For example, if embedding into an environment (e.g. a database) which
has a native array type (such as SQL arrays) which can be iterated
sequentially more efficiently than they can be indexed into (because
they have variable-length elements), it means that you can't just
provide an __ipairs and have everything just work in the obvious way;
instead, ipairs() will work inefficiently and you have to provide a new
but functionally identical method.

--
Andrew.