lua-users home
lua-l archive

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


Thanks Ross, I will try that out.

Is there any reason why the assignment operator cannot be overloaded in Lua?

Abhijit

On Thu, Dec 24, 2015 at 2:31 PM, Ross Bencina <rossb-lists@audiomulch.com> wrote:


On 24/12/2015 6:21 PM, Abhijit Nandy wrote:
I would love to be able to say:
int 'm' = 3

But putting the '=' breaks the syntax. Is there any way to enable this
kind of syntax?

Just an idea:

I don't think that you can overload the assignment operator, but maybe you could overload '<='. e.g.:

int 'm' <= 3

I think you'd need to make defaultValueParser a table with a __call metamethod. (I don't think you can set a metatable on a plain function.)

>>>>>>>>>

defaultValueParser = {}

mt = {
  -- put your old defaultValueParser implementation in __call:
  __call = function(self, x) print(">", x, "<") end,
  __le = function(lhs, rhs) lhs(rhs); end -- invoke __call
}
setmetatable(defaultValueParser, mt)

int = function(y) return defaultValueParser end

----
-- example usage:

x = {
  int 'm',
  int 'm'(10),
  int 'm' <= 8
};

<<<<<<<<<<


http://www.lua.org/manual/5.2/manual.html#2.4
http://lua-users.org/wiki/MetamethodsTutorial

Ross.