Results 1 to 1 of 1

Thread: Ew ew ew ew ew (horrible Lua design flaw.)

  1. #1
    Lesser Knight
    Join Date
    Dec 2006
    Location
    United States
    Posts
    47

    Angry Ew ew ew ew ew (horrible Lua design flaw.)

    Lua always creates a new function object for each inner function whenever the outer function is invoked instead of optimizing it away when possible. (Okay, this isn't really a big deal now that I think about it.)

    Because my class() thingy used a closure I made a version that doesn't. It's probably more readable anyway.

    Code:
    do
        local function call(cls, ...)
            instance = setmetatable({}, cls)
            if cls.__init then
                cls.__init(instance, ...)
            end
            return instance
        end
    
        function class()
            local cls = {}
            cls.__index = cls
            return setmetatable(cls, {__call = call})
        end
    end
    Note: because of bizarre scoping rules "call" has to precede "class". Do you get a decent error message if you do otherwise? Yeah, right.
    Last edited by Ian; 02-24-2007 at 11:54 PM.

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •