[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: How to define ranges for tables?
- From: "Jasper Klein" <jasper@...>
- Date: Sat, 09 Oct 2021 23:34:41 +0200
The functions string.byte, string.sub, table.concat, table.move, etc. accept indices to the begin and end as parameters of a range in a table on which the function is applied. The end index is 'including' so this means its not possible to define a zero length range.
When the begin index is passed the end index, it seems that the functions do nothing. So, there is no way to define a 'reversed' range.
For someone that likes C++ as much as Lua, using indices as is common in the standard Lua functions, feels limiting compared to C++ iterators.
With C++ iterators it is possible to define a zero length ranges and reversed ranges (with reverse iterators).
The reason for bringing this up is that I'm writing a Lua module (in C++) to manipulate tables.
I want to give the functions in this module an API with a standard Lua module 'look and feel' but also C++ iterator-like possibilities.
At the moment I have two options:
1) Define a range by an index and a size. A reversed range has a negative size. 'end offset' instead of 'size' might be a better definition because negative sizes is a bit weird.
This concept is not used by the standard Lua functions.
2) Use begin and end indices as range definition as already used in Lua. Let the user handle the 'zero length' case.
Allow the begin index to be passed the end index. In this case the sequence starts at the begin index and counts down until it has reached the end (including).
* Which option do you like most for usage in an API?
* What would you do to define a range in Lua?