lua-users home
lua-l archive

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


lhf quotes himself:

 There is exactly ONE spot in the core of Lua where this is true: the
 parsing of constructors such as {10,20,30} . The convention of starting
 with 1 is only enforced by the libraries, which are not part of the 
language.
 http://lua-users.org/wiki/CountingFromOne

Actually, the varargs syntax for function calls creates a 1-indexed table, 
which is sort of the same thing but I think it is technically a different 
"spot".

However, once you have those, the library pretty well has to follow along. 
Otherwise, for example, table.foreachi({x, y, z}, fn) is a no-op. :) 

Not that I'm complaining. I have no trouble with 1-indexing. I actually 
like it. It means that, for example, the size of a collection and the 
index of the last element of the collection are the same number (eg. 
strings, vectors created with table.insert). (But this is not a troll -- I 
know lots of folk grudgingly accept it and I don't want to get in an 
argument with anyone.)

In any event, the original post had to do with Python's *syntax*, not its 
*semantics*. Here I am prepared to take a stronger stand: I don't like 
indentation as a syntactic element. It makes it too difficult to rearrange 
code in a text editor.

By the way, speaking of indentation, I think I tried to survey the list on 
this quite a while ago, but I don't recall getting much feedback. The 
question is: how do you prefer to indent "if" statements:


--Alternative 1:
if a == 0 then
  print "a is zero"
  sign = 0
elseif a < 0 then
  print "a is negative"
  sign = -1
else
  print "a is positive"
  sign = 1
end

--Alternative 2:
if a == 0 then
    print "a is zero"
    sign = 0
  elseif a < 0 then
    print "a is negative"
    sign = -1
  else
    print "a is positive"
    sign = 1
end

--Alternative 3:
if a == 0 then
  print "a is zero"
  sign = 0
 elseif a < 0 then
  print "a is negative"
  sign = -1
 else
  print "a is positive"
  sign = 1
end

--Other alternatives ?

I'm trying to work my way through vim's indenting rules, and I was 
wondering what people thought. I used to use alternative 2 because it is 
clearer where the end and the if match. But it uses more horizontal space, 
but it seems wierd when there are no elseif/else clauses:

if a > b then
    a, b = b, a
end
while a < b do
  a = (a + b) / 2
  print(a)
end

I'm kind of drawn to alternative 3, but it means getting vim to 
co-operate. (I don't mean to start a flame-war about text editors, either. 
I happen to like vim, but it's a very personal thing.)