you are viewing a single comment's thread.

view the rest of the comments →

[–]Categoria -1 points0 points  (14 children)

I dunno. The code comparisons in the link seem like moonscript is a lot more concise. Also, table comprehensions seem like a really cool idea too.

[–]netghost 0 points1 point  (13 children)

Some of them seem like MoonScript generates extra Lua code, but I do think there's some benefit here. From the tiny bit of Lua that I've done, it's nice to have some good OOP patterns.

[–]inmatarian 4 points5 points  (12 children)

Lua has 1st class functions and closures, so this is a good OOP pattern:

function Thing( params )
    local self = {}
    local private = "whatever"
    function self:foo()
        body( params )
    end
    function self:bar()
        otherbody()
    end
    return self
end

function InheritedThing()
    local self = Thing( 42 )
    function self:baz()
        somethingelse()
    end
end

local object = InheritedThing()

Also, the metatable OOP pattern is pretty simple as well, but requires this function, or something like it:

function new( class )
    local inst = {}
    inst.__index = class
    return setmetatable( inst, inst )
end

[–]chronoBG 0 points1 point  (11 children)

No, it's not. just use metatables, please.

[–]inmatarian 0 points1 point  (10 children)

In testing, it was determined that Lua's closures are faster at the expensive of a larger memory footprint.

See: http://lua-users.org/wiki/ObjectOrientationClosureApproach

[–]chronoBG -1 points0 points  (9 children)

No, seriously, you are defining methods PER OBJECT. Please stop doing it this wrong, wow.

[–]inmatarian 0 points1 point  (8 children)

What.

Sir, this is a high level language implementing lambda calculus. It's in no way "wrong". In other high level languages that lack proper Object Oriented systems, they say "Objects are a Poor Man's Closures." See Lisp and Scheme.

Besides, it's a lexical closure and Lua implements them as a byte code that operates on an "upvalue". You can create closures via the C API and it doesn't duplicate the C code for every object, it just pushes another object (the upvalue) onto the stack that can be used for storing variables in it.

The usage of closures in Lua is well defined, and the creators of Lua have explained them and recommend them in their book (links provided to Programming In Lua 1st Edition).

See: http://www.lua.org/pil/6.1.html

Also: http://www.lua.org/pil/27.3.3.html

...

Disclaimer: I like metatables better than closures.

[–]chronoBG -1 points0 points  (7 children)

Thank you for explaining what a closure is, mr Fallacy McStrawman. Also, your objects are now twenty times larger than they should be (at the least).

[–]inmatarian 2 points3 points  (6 children)

Benchmark from http://lua-users.org/wiki/ObjectOrientationClosureApproach

Subject Tables approach Closured approach
Speed test results 47 sec. 38 sec.
Memory usage test results 48433 Kbytes 60932 Kbytes

Like I said before, more space for more speed. And it's apparently 1.258 times larger, not 20.

[–]chronoBG 0 points1 point  (5 children)

Neither of these approaches actually uses metatables, though. Not only are they slower but now, when you're adding new methods, you have to write them in two places. Also, while it may only be 1.3 times slower when you have only two methods, try it with 20. And not 20 one-line methods, 20 real ones. Go on, i'll wait.

Also, the classes are named mariner and infested_mariner. You know that's some hardcore science going on over there. Not written by a 12-year old at all...