Thread Tools Display Modes
08/24/14, 10:19 AM   #1
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Functions, Scope, Reference, Or Copy

I see different people writing code in different ways.

Question 1:
Is there some purpose for doing the following (other than to be able to juse a different name (c1) to call the function)? And when you do that in lua is c1 now a copy of the function or a reference to the original function?
Lua Code:
  1. function myFunction()
  2.     ...do stuff here
  3. end
  4.    
  5. c1 = myFunction()

Question 2:
or how about this, what is the purpose of making the function local, if you then create c1 which gives global access to the function?
Lua Code:
  1. local function myFunction()
  2.     ...do stuff here
  3. end
  4.    
  5. c1 = myFunction()

Question 3:
Does the reference/copy thing work the same way for libraries?
If I am using a library in several files of myAddon, which would be best:

I wanted to do this in each file so each file has its own local (?reference/copy?)..., but I wasn't sure if LIB is a copy of the library or a reference to the library. If its a copy that means I would be loading a copy of the library for every file I used it in.
Lua Code:
  1. -- Do this in every file --
  2. local LIB = LibStub:GetLibrary("SomeLibrary-1.0")


Or if it creates a copy, I assume I should only grab it once and just leave that reference global like this?
Lua Code:
  1. -- Do this once --
  2. MyAddon.LIB = LibStub:GetLibrary("SomeLibrary-1.0")
and use MyAddon.LIB in every file.
  Reply With Quote
08/24/14, 11:51 AM   #2
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
A1: Assignment makes references, not copies.
You probably meant
Lua Code:
  1. c1 = myFunction -- no parentheses, that would call myFunction
Now c1 is a reference to the same object as myFunction (which is a reference, too); the referenced object is the function.
Lua Code:
  1. function foo() print("foo") end
  2. bar = foo
  3. function foo() print("bar") end
  4. -- now bar is a reference to the first function, which prints "foo"
  5. -- and foo is a reference to the second function, which prints "bar"

A2: I do this when the function is recursive.
For example:
Lua Code:
  1. local function tostrings(...)
  2.     if select("#", ...) > 0 then
  3.         return tostring(...), tostrings(select(2, ...))
  4.     end
  5. end
  6.  
  7. myAddon.tostrings = tostrings
1) access to local variable is supposedly faster. If I defined it as function myAddon.tostrings(...), and in the body called myAddon.tostrings -- that call would require 2 hashtable lookups -- first _G["myAddon"] and then myAddon["tostrings"]; whereas with local, it's fast upvalue access by index.

2) another difference is that you can't replace local functions from the outside. Which is pretty annoying when you want to modify ZOS functions and they're local I can't come up with a sensible example right now, but: if I write a function that calls a bunch of smaller functions to do its work; and then someone else (from a different addon) wants to replace one of those smaller functions to slightly modify the behaviour of the big function, they won't be able to do it if the smaller function is local.

A3: Just use local LIB = LibStub:...
It's cleaner, doesn't depend on load order (having Addon.LIB you have to make sure LIB is set before any file uses it), and with local LIB you'll save 2 hashtable lookups with each call into LIB; for a tiny initialization price.
  Reply With Quote
08/24/14, 12:09 PM   #3
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Awesome, makes perfect sense.

Originally Posted by merlight View Post
You probably meant
Lua Code:
  1. c1 = myFunction -- no parentheses, that would call myFunction
Now c1 is a reference to the same object as myFunction (which is a reference, too); the referenced object is the function.
I intentionally put those parenthesis on there because I was reading "Lua Programming in Lua" & copied it off their web site. http://www.lua.org/pil/6.1.html

But I looked at it again and see that I misunderstood what they were doing. Their function returned a function that is why they were using the extra parenthesis. But regardless of what they were doing your answer is what I was looking for.

Thanks
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Functions, Scope, Reference, Or Copy


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