WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   A question for the Lua experts (https://www.wowinterface.com/forums/showthread.php?t=52285)

Digital_Utopia 05-08-15 09:53 PM

A question for the Lua experts
 
I'm not very knowledgeable about what goes on underneath Lua's hood, so I'm hoping that someone who is, can clear something up for me.

It seems that no matter where I go, any discussion regarding pseudo namespaces/"static" classes in Lua, always boils down to something like this:

Code:

foo = {}
foo.a = "something"
foo.b = function() return "bob" end

I mean, basic - straightforward assignments.

But, see - I was playing around today, and noticed that it's possible to pull off a Javascript Module Pattern in Lua.
eg:

Code:

foo = (function()
    local a="something";
    function b()
          return "bob";
    end
    return{
          ["a"] = a,
          ["b"] = b,
    }
end)();

I mean obviously, for something as simple as the example, it's a bit overkill - but why isn't this a thing? Is there some performance hit involved? Or is it just a case of what people are used to?

jeruku 05-08-15 10:12 PM

From what I have seen here it is just a case of what people are used to using.

semlar 05-08-15 10:41 PM

Calling a function is one of the slowest things you can do in lua, and in your example you create a function which is only used to create a table and thrown away.

What do you gain by writing it this way?

Digital_Utopia 05-08-15 11:32 PM

Quote:

Originally Posted by semlar (Post 308711)
Calling a function is one of the slowest things you can do in lua, and in your example you create a function which is only used to create a table and thrown away.

What do you gain by writing it this way?

Well, mostly a combination of OOP and structure/organization. Useful for "static" classes, as there's only one instance, as opposed to using some of the actual "class" examples, which also use an additional function to initialize.

Also like those so-called classes, you have the ability to control private or public methods/properties, as you only expose what you return.

The question I guess, would be - would it be any slower than something like this?

Code:

foo = {};
function foo:new()
    local self = {};       
    self.a = "Something";
    self.b = function() return "bob" end
    return self;           
end

local bar = foo:new();

Which is the common method of making "classes" in Lua, when unique instances are required.

Banknorris 05-09-15 01:34 AM

Lua supports OOP, see this:
http://lua-users.org/wiki/ObjectOrientedProgramming
in particular you may be more interested in
http://lua-users.org/wiki/ObjectOrie...losureApproach


All times are GMT -6. The time now is 04:54 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI