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.
>>> 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.
s/first/last/, the arguments will be shifted to the right. But yes, it can be annoying.
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.
This is false for Python:
>> print bla
NameError: name 'bla' is not defined
In Lua:
> = bla
nil
setmetatable(_G or _ENV, {__index = error})
-- `error` for the example, you can write a function that prints a better error message.