Quantcast
A question for the Lua experts - WoWInterface
Thread Tools Display Modes
05-08-15, 09:53 PM   #1
Digital_Utopia
A Flamescale Wyrmkin
 
Digital_Utopia's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2012
Posts: 110
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?
__________________
  Reply With Quote
05-08-15, 10:12 PM   #2
jeruku
A Cobalt Mageweaver
 
jeruku's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 218
From what I have seen here it is just a case of what people are used to using.
  Reply With Quote
05-08-15, 10:41 PM   #3
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,059
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?
  Reply With Quote
05-08-15, 11:32 PM   #4
Digital_Utopia
A Flamescale Wyrmkin
 
Digital_Utopia's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2012
Posts: 110
Originally Posted by semlar View Post
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.
__________________
  Reply With Quote
05-09-15, 01:34 AM   #5
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
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
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » A question for the Lua experts

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off