Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. -- This works: "bar()" references a global by default, and the next block sets
  2. -- a global variable.
  3. function foo()
  4. bar()
  5. end
  6. function bar()
  7. print("bar")
  8. end
  9.  
  10. -- This DOESN'T work: "bar()" references a global because Lua hasn't seen
  11. -- anything to the contrary /yet/, and then the next block creates a local, so
  12. -- the global "bar" is never defined, and you get an error about trying to call
  13. -- a nil value.
  14. local function foo()
  15. bar()
  16. end
  17. local function bar()
  18. print("bar")
  19. end
  20. ]]
  21.  
  22. -- This ALSO DOESN'T work: "bar()" references a local, because it appears after
  23. -- "local bar", but "local function bar()" creates a NEW local whose scope
  24. -- begins at that point, so the earlier "bar" is a different variable that's
  25. -- never assigned.
  26. local bar
  27. local function foo()
  28. bar()
  29. end
  30. local function bar()
  31. print("bar")
  32. end
  33.  
  34. -- This DOES work: "bar()" references a local, and later on that local is
  35. -- assigned normally. Unfortunately, it's very misleading if the "local"
  36. -- statement appears some ways before the actual definition of "bar", which
  37. -- would then look like a global.
  38. -- On the other hand, luacheck complains if you create any globals at all, so
  39. -- if you're using it, you can safely assume nothing is global? I guess?
  40. local bar
  41. local function foo()
  42. bar()
  43. end
  44. function bar()
  45. print("bar")
  46. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement