4 ms·
The 1-based indexing isn't particularly bothersome. It's the method vs. function determination at call site combined with default nil for unspecified arguments
by vbit 11y ago
The 1-based indexing isn't particularly bothersome. It's the method vs. function determination at call site combined with default nil for unspecified arguments that really gets you.
- haberman 11y agoI've never had any problem with the two things you mentioned.
- jnbiche 11y ago> default nil for unspecified arguments Isn't this what most scripting languages do? I know it's the case with Python and PHP. And I disagree about the 1-based array indexing. With algorithms that require a lot of 1-off values, it can be a huge pain in the ass, particularly if you're trying to integrate it with C (and its 0-based array indexing). Still not sure why the creators chose it, given that Lua was designed for easy and close C interop.
- ewmailing 11y agoWhy? From one of Roberto Ierusalimschy's talks (starts at slide 6): http://www.inf.puc-rio.br/%7Eroberto/talks/ws2011.pdf http://www.inf.puc-rio.br/%7Eroberto/talks/ws2011.pdf Excerpt: - Not all languages are 0-based: Icon, Fortran, AWK1, and Smalltalk are 1-based; Snobol, Pascal, Modula, Modula-32, and Ada have configurable bases. - Currently, many languages are 0-based due to influence from C. - Ironically, none of them share the reason that made C 0-based (where a[e] means *(a+e)). 1-based arrays: - Much more intuitive: first is 1st (not 0th). ISO-C: “E1[E2] designates the E2-th element of E1 (counting from zero).” - Much easier for non programmers. Easy for (good) programmers :) - Historical reason: Fortran used 1-based arrays, and most first users of Lua had a Fortran background.
- kbd 11y agoThe presentation asserts that 0-based arrays have "More interesting mathematical properties". Dijkstra's classic paper on this is a must-read: http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
- rkrzr 11y ago> Isn't this what most scripting languages do? I know it's the case with Python and PHP. This is false for Python: >> print bla NameError: name 'bla' is not defined In Lua: > = bla nil
- pygy_ 11y agoThat's yet another source of nil, but it can be fixed with modules like strict.lua, or simply setmetatable(_G or _ENV, {__index = error}) -- `error` for the example, you can write a function that prints a better error message.
- vbit 11y agoPython errors out if you don't specify all arguments. >>> def f(a): ... pass ... >>> f() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes exactly 1 argument (0 given) >>> I agree 1-based indexing isn't ideal, but in the grand scheme of things, the default nils and the method/function syntax are a constant source of mistakes and frustration for me. In Lua you have to call `obj:method()` which to pass `obj` as the first argument. If you accidentally do `obj.method()` - there is no error, but the first argument becomes `nil`. In Python, there the call syntax is consistent and `obj` is automatically passed as the first argument for methods.
- pygy_ 11y ago> but the first argument becomes `nil` s/first/last/, the arguments will be shifted to the right. But yes, it can be annoying.
- jnbiche 11y agoThanks for the corrections. I've been switching a lot between Lua and Python for the past year and have clearly gotten some things mixed up.