View Single Post
08-16-14, 07:37 PM   #1
Malakahh
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2009
Posts: 30
Return outside a function?

Looking at the basic example for using LibStub (http://www.wowace.com/addons/libstub/#w_basic-example), I've encountered a return statement that I don't quite understand, as it is outside of any function.

Lua Code:
  1. local lib = LibStub:NewLibrary("MyLibrary-1.0", 1)
  2.  
  3. if not lib then
  4.   return    --HERE, NOTICE ME
  5. end

This leads me to think that addons are run as functions, and this return statement will terminate the addon.
I wrote a little test to confirm this theory:
load order: file1, file2

file1:
Lua Code:
  1. print("zero")
  2.  
  3. --Removing this if statement causes something called a Tail Call (this is new to me as well), which will result in "zero" and "one" getting output, but not file2.
  4. if 1 == 1 then
  5.     return
  6. end
  7.  
  8. print("one")

file2:
Lua Code:
  1. print("two")

After running this code, only "zero" gets output, confirming my suspicion.

However, I'm not sure if my suspicion covers everything here and the addon simply gets terminated, or there are implications for using this that I don't understand. I would appreciate if anyone could elaborate, since my googling haven't been helpful.
  Reply With Quote