> Sadly comparisons like < and == cannot be overloaded in Lua :(
You can overload == in 5.3. Before that, there was a slightly-less-overloadable __eq method. But as of 5.3, as long as the bound __eq method is the same underlying function, it will be called.
I assume you meant instead, that the return value of the comparison is always coerced to a boolean, so you can't re-use it to get arbitrary infix operators?
For that sort of thing, I've used Mike Pall's hack in the past:
local sm = setmetatable
local function infix(f)
local mt = { __sub = function(self, b) return f(self[1], b) end }
return sm({}, { __sub = function(a, _) return sm({ a }, mt) end })
end
local shl = infix(function(a, b) return a*(2^b) end)
print(5 -shl- 4)
[3 hidden]5 mins ago
Nice! Yes I meant the type is coerced to bool. For me the a:eq(b) syntax is already good enough and less hacky
[3 hidden]5 mins ago
Tarantool[1] has been using Lua for the queries since inception. Eventually 3rd party users complained enough that they added a SQL as a query frontend.
You can do this with Ruby and Elixir too, and the syntax would end up much nicer!
[3 hidden]5 mins ago
Lua is nice because it can be embedded anywhere with very little overhead, but in my experience it’s very easy (and fun) to port Prela to most languages
[3 hidden]5 mins ago
Well, that's a matter of opinion. I find Lua syntax to be quite nice already.
You can overload == in 5.3. Before that, there was a slightly-less-overloadable __eq method. But as of 5.3, as long as the bound __eq method is the same underlying function, it will be called.
I assume you meant instead, that the return value of the comparison is always coerced to a boolean, so you can't re-use it to get arbitrary infix operators?
For that sort of thing, I've used Mike Pall's hack in the past:
[1]: https://en.wikipedia.org/wiki/Tarantool
It's built over DuckDB and Rayforce[2]. Single query, either engine.
[1] https://github.com/jennystats/janet-query
[2] https://github.com/RayforceDB/rayforce