lua-users home
lua-l archive

Re: lists with nil play nice for Lua 5.2

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


actually this does cause a bit of confusion.
We've got an interface that allows people to manipulate data in a DNS packet which is housed in a C++ object. since DNS packets can have numerous records in them, there needs to be a way to index them. Doing this from Lua then becomes confusing, since accessing the C++ object from Lua uses 0-based indexing, but accessing any other normal array/table in lua uses 1-based indexing. I'm not looking forward to explaining that to users. Our policy control system has a script-based option, which calls a Lua function which returns a bool. The user can fill out the body of the function, so we have the situation like
function Filter(Request)
 if (Request.Query[0].Name == "www.microsoft.com") then
 Request.Query[0].Name = "www.wingate.com";
 return true;
 end
 return false;
end
Since we have __index metamethod on the table represented as "Request", we get a callback with a name and offset which we can resolve. I guess we could just subtract 1 from the index number passed up and use "native" lua indexing .... I do agree that when you are specifying a for loop with a sequence defined as "1,N" then it makes a lot more sense to have "1,N" rather than "0,N-1". It's more a function of how the syntax of the for loop works which then passes over to table indexing.
John Belmonte wrote:
Jerome Vuarand wrote:
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
In this paper he says that 0-based indexing is better if you use
convention 'a' (loops of the form 'for (i=0; i<N; ++i)'). Lua uses
convention 'c' (loops of the form 'for (i=1; i<=N; ++i)', or in Lua 'for
i=1,N do'), so 1-based indexing is more suited to Lua.
That is a recursive argument. Lua uses convention "c" because it has
1-based indexing. The choice of index base drives how you specify
ranges. No sane language or library would mix 1-based indexing with
half-open ranges, or 0-based indexing with closed ranges.
--John
--
Adrien de Croy - WinGate Proxy Server - http://www.wingate.com

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