[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Compatible coding: 'arg' in 5.1 vs 5.2
- From: Dirk Laurie <dpl@...>
- Date: Fri, 3 Jun 2011 12:25:01 +0200
The Lua 5.2 manual, Section 8, does not list the following
incompatibility:
There is no local variable "arg" in a vararg function anymore.
One now has three options when writing code that must run correctly under
both 5.1 and 5.2:
1. Think in Lua 5.1 but adapt the code to be correct under 5.2 too.
2. Think in Lua 5.2 but adapt the code to be correct under 5.1 too.
3. Think in both and do explicit tests on _VERSION.
Sample code to illustrate what I mean in the first two cases:
~~~
table.pack = table.pack or function(...) return arg end
arg = {'a','b','c','d'}
function test1(a,...)
local arg = table.pack(...) -- not actually needed under 5.1
print(arg.n,arg[1],arg[2],arg[3])
end
function test2(a,...)
local arg = _G.arg -- not actually needed under 5.2
print(#arg,arg[1],arg[2],arg[3])
end
test1(1,2,3,4) --> 3 2 3 4
test2(1,2,3,4) --> 4 a b c
~~~
Which is better?
Dirk