14 points remywang 1 hour ago 4 comments
phillc73 1 hour ago | parent
A little while ago I had a similar itch, but my query language at home happened to be Janet[1].
It's built over DuckDB and Rayforce[2]. Single query, either engine.
shakna 1 hour ago | parent
> 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)remywang 46 minutes ago | parent
Nice! Yes I meant the type is coerced to bool. For me the a:eq(b) syntax is already good enough and less hacky
behnamoh 38 minutes ago | parent
You can do this with Ruby and Elixir too, and the syntax would end up much nicer!