I'd like to be able to make a program for ComputerCraft using MoonScript, but due to the way that CC is sandboxed to prevent security issues on Minecraft servers and such, I can't require moonscript directly and run moonscript code from there. I have to convert my moonscript code to lua.
This is problematic, however, due to the fact that the class implementation for moonscript is very big, and not very filesize-conservative. When I type "class Bacon", it outputs this for lua:
local Bacon
do
local _parent_0 = nil
local _base_0 = { }
_base_0.__index = _base_0
if _parent_0 then
setmetatable(_base_0, _parent_0.__base)
end
local _class_0 = setmetatable({
__init = function(self, ...)
if _parent_0 then
return _parent_0.__init(self, ...)
end
end,
__base = _base_0,
__name = "Bacon",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil and _parent_0 then
return _parent_0[name]
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
Bacon = _class_0
return _class_0
end
And this is for every class implementation, which is kind of ridiculous. Is there any way I can shorten this in my moonscript code?
The amount of generated code has been cleaned up quite a bit in the latest version, 0.2.4: http://leafo.net/posts/moonscript_v024.html#code_generation_changes
A class now minimally looks like this