Advertisement
Guest User

explanation

a guest
Apr 29th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #### 1. You really don't need to be worrying about "lag and performance." At this point you're misusing these terms.
  2.  
  3. # 2. There are no different "types" of functions in Lua.
  4.  
  5. Functions are their own data type, kind of like how strings and tables and so on are their own data type.
  6.  
  7. The only differences of course would be how they're being used.
  8.  
  9. Your first example is syntactic sugar (a nicer way to write something else) for
  10.  
  11. ```lua
  12. local This
  13. This = function(x)
  14. print(x)
  15. end
  16. ```
  17.  
  18. This makes `This` (no pun intended) an upvalue (external local variable) inside the function scope. This also allows for recursion. Your second example would not allow recursion.
  19.  
  20. Let me use a famous example of recursion
  21.  
  22. ```lua
  23. local function fact(num)
  24. if (rawequal(num, 0)) then
  25. return 1; --// 0! = 1
  26. end
  27. return num*fact(num - 1);
  28. end
  29.  
  30. print(fact(5)); --> 120
  31. ```
  32.  
  33. But if we wrote it in your second example's format:
  34.  
  35. ```lua
  36. local fact = function(num)
  37. if (rawequal(num, 0)) then
  38. return 1; --// 0! = 1
  39. end
  40. return num*fact(num - 1);
  41. end
  42.  
  43. print(fact(5));
  44. --> attempt to call a nil value (global 'fact')
  45. ```
  46.  
  47. There is an "attempt to call a nil value" error.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement