2016-05-31 14:42 GMT+02:00 steve donovan <steve.j.donovan@gmail.com>:
On Tue, May 31, 2016 at 2:37 PM, Alexey Melnichuk
<alexeymelnichuck@gmail.com> wrote:
May think about short from to define function.
Believe me, I tried ;) Such proposals have never flown far.
Maybe because this is already available, though not standardized.
The following code runs on Lua 5.1, Lua 5.2, Lua 5.3 and LuaJIT 2.0.
getmetatable"".__pow =
function(args,result)
return (loadstring or load)("local "..args.."=...; return "..result)
end
print (("x,y,z" ^ "(x-y)/z, (x+y)/z")(3,4,5)) --> -0.2 1.4
The only restriction is that tonumber(args) and tonumber(result)
must be nil. This is no problem for args and can be circumvented
easily for result, e.g. "x,y" ^ "(1)".
In Lua 5.3, I prefer to use other metamethods to allow for default
arguments with the same look.
getmetatable"".__bxor =
function(args,result)
return (loadstring or load)("local "..args.."=...; return "..result)
end
getmetatable"".__bnot =
function(result)
return load("local x,y,z=...; return "..result)
end
(~"x+y,-x+y")(3,5) --> 8 2