pigpigyyy / Yuescript

A Moonscript dialect compiles to Lua.
http://yuescript.org
MIT License
424 stars 35 forks source link

Allow to use metatable operators for exporting #133

Closed SkyyySi closed 1 year ago

SkyyySi commented 1 year ago

I think it would be useful if we could use things like this:

export <name> = "My module"
export <call> = (...) -> -- ...

In some Lua environments, like Awesome WM, it is common practice to use modules as both namespaces and as a constructor.

pigpigyyy commented 1 year ago

Added a different syntax to support export metamethods for a module by

export.<name> = "My module"
export.<call> = (...) -> -- ...

that compiles to

local _module_0 = setmetatable({ }, { })
getmetatable(_module_0).__name = "My module"
getmetatable(_module_0).__call = function(...) end
return _module_0

Because the statement exporting item with assignment will always leave a local variable. A new syntax for disabling local variable creation when doing export could be a better way for metamethods (Or we may get messy local variables named __call, __eq and etc).