lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Joe Krahn wrote:
> Is anyone else thinking about or working on something like this?

I posted some ideas about this, inside some other discussion on

BTW: If you don't care too much about efficiency then check out
this simple hack:

  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 })

Now you can define your own named infix operators:

  local shl = infix(function(a, b) return a*(2^b) end)

  print(5 -shl- 4)
  --> 80

Cute, huh? Advantage: no changes to the Lua core needed.
Drawback: one table allocation per operation.

[Credits: this mimicks a similar Python hack: