3 ms·
>>OTOH, from-1 indexing is evil. This is a straw man. You're not forced to index[1]. You can create tables that index[0], no worries.
by fit2rule 11y ago
>>OTOH, from-1 indexing is evil.
This is a straw man. You're not forced to index[1]. You can create tables that index[0], no worries.
- munificent 11y agoExcept the standard libraries, all third party libraries, and every existing Lua user maintaining your code will assume 1-based indexing. Cars don't require you to drive on the right side of the road either (or whatever side your nationality prefers), but you're going to have a bad time in traffic if you try to switch.
- fit2rule 11y agoIf you need 0-based arrays, use them. Its no big deal. Any Lua programmer worth their salt knows that Lua uses 1-arrays - so? Set up a 0-array and off you go.
- qwertyuiop924 11y agoEither you're an idiot, or you're being sarcastic. I will assume the ladder.
- fit2rule 11y agoIn fact, I'm learning a lot from this thread, particularly about idiocy and sarcasm, although very few actual real arguments for why array[1] is 'inferior' to array[0], because in fact there is no such valid argument. Lua standard libraries do not suffer this problem. If you have a legacy / cross-platform issue about it, well then .. its a good thing one is 'wise enough to know array[0] is a thing', but its hardly relevant to the question of whether this 'inferior accessing of arrays in Lua' is anything more than fallacy. Idiomatic Lua involves intelligent use of pairs/ipairs/table index schema, and metatables if needed, to solve the data-access problem. And guess what? A competent Lua developer either has had zero problems with this issue (because they are competent and know how to wire up a C/C++ data structure to the Lua vm), or they have infinite, multiple "problems with Lua", of their own devising, nevertheless, but hey .. you can't "blame Lua" for that. Lua gives you the keys to the universe. If you crash into someones mailbox, well then ..
- LewisJEllis 11y agoSure, technically you /can/ do it, but only in the same way that you can use negative indices in JavaScript arrays and objects. The 0-indexed element won't tie into length operators without additional manual bookkeeping with table.setn. $ lua Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio > x = {} > x[0] = 1 > =#x 0 > =x[0] 1 > x[1] = 2 > =#x 1 > $ node > x = [] [] > x[-1] = 0 0 > x[0] = 1 1 > x [ 1, '-1': 0 ] > x.length 1 That's reason enough IMO to just suck it up and use 1-based indexing, even before you add munificent's (stronger) point about standard libraries and the rest of the ecosystem. I agree that 1-based indexing isn't as much of a downside as people make it out to be, but saying you can ignore it and index from 0 "no worries" is just being silly.
- qwertyuiop924 11y agoIn lua, iterating over arrays/tables with things like ipairs() is the idiom, so it's not so bad, but if you want to do anything involving mod, you have to add an extra plus 1. this is where bugs happen.
- LewisJEllis 11y agoYes, ipairs is idiomatic, but that doesn't matter here because ipairs doesn't return the 0 index. > x = {} > x[0] = 2 > x[1] = 3 > for i, v in ipairs(x) do print(i, v) end 1 3 > x[2] = 4 > for i, v in ipairs(x) do print(i, v) end 1 3 2 4
- qwertyuiop924 11y agoNo, what I mean is that it's not idiomatic to access lua "arrays" by index.
- fit2rule 11y agoThe only requirement is the Lua developer knowing how to use a Lua table for anything they need.