On Jul 8, 2013 2:35 AM, "Miles Bader" <firstname.lastname@example.org> wrote:
> One thing I've run into with Lua 5.3-work1 is that I want to make code
> which is portable between different versions of Lua, and while this is
> usually pretty easy by simply using runtime tests, one can't use
> anything which is invalid syntax in earlier Lua versions, like the
> "//" operator.
> For instance, I can use the following style of "adaptable" code:
> -- A version of floor that returns a true integer value in Lua 5.3.
> local ifloor = math.ifloor or math.floor
> To define an "idiv" function, which is basically math.floor(a/b), I'd
> like to use the "//" operator in Lua 5.3, as it may be more efficient.
> But I'm not sure how to write this, because 5.2 won't accept the "//"
> syntax, even if the code is never executed.
> For that reason, it would be nice if Lua 5.3 came with a standard
> "math.idiv" function, which basically just returns a//b...
> Then I could just write my own version adaptable local function like:
> -- Returns math.floor (a / b)
> local function idiv (a, b)
> if math.idiv then
> return math.idiv (a, b)
> return ifloor (a / b) -- ifloor from above
> Bacchus, n. A convenient deity invented by the ancients as an excuse for
> getting drunk.
local f = loadstring("return function(a, b) return a // b end")
if f then idiv = f()
else idiv = function(a,b) return ifloor(a / b) end