Hacker News new | ask | show | jobs
by RyanSquared 3306 days ago
This is basically Lua, excluding type annotations. Lua's form of OOP is very strange in that methods aren't bound to a parent context, but instead take the object as a value. For example, `object:method()` is the same as `object.method(object)`, and `function object:method()` is the same as `function object.method(self)` - the only difference being calling the function via `object:method()` is a bit more optimized. There is also a framework with networking, signals, and other cool features called `cqueues` which works based off of coroutines.s You might want to check that out too :)
2 comments

And the size of the Lua runtime is nothing short of beautiful. I have yet to find a use for Lua though, so I'm still waiting for an opportunity to work on something with it.
I find it's used a lot in places you wouldn't expect. I found it used in a car radio, the modem used in my house, and a few other places. It's lightweight if you want to use it on embedded systems (for example, NodeMCU) if you're into that kind of thing.
Iirc its used on some digital cameras as well.
I'm using it at work to parse SIP messages (with LPeg) for one of our customers (one of the cellphone carriers). Between LPeg for parsing, and coroutines in Lua, the code is pretty easy to read and follow.
I forgot to emphasize that I'm particular about syntax. I amended my comment accordingly. I also don't like the global-by-default bit, but maybe my ideal language could be a syntax layer atop Lua?
Metatables are your friend.

    -- LuaJIT
    
    local oldG = _G
    local newG = setmetatable({}, {
        __index = oldG,
    })
    setmetatable(oldG, {
        __index = function(t, k)
            return rawget(newG, k)
        end,
        __newindex = function()
            error('attempt to assign global variable')
        end,
    })
    _G = newG
If you want to assign a global variable, `x = 5` will error but `_G.x = 5` won't.
If you ignore the class support of moonscript, you can pretty much use it to do what you want. It doesn't have the "global by default" silliness, to start with.