Thread: SellAllJunk()
View Single Post
12/17/14, 02:40 PM   #4
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
Sure it's useful for how a language behaves. It is correct if you look at the Lua manual that functions are stored as references/closures. However, when you assign a new function, you overwrite the reference not the function itself. For most users, treating this as pass-by-value is fine.

Consider this example:
Lua Code:
  1. foo = function() print "Original" end
  2. bar = foo
  3. foo = function() print "Second" end
  4.  
  5. foo()
  6. bar()

Output:
Code:
"C:\Program Files (x86)\Lua\5.1\lua.exe" functionRef.lua
Second
Original

Process finished with exit code 0
The copy (bar) is not updated when the original (foo) is changed, because you're overwriting the reference not the underlying function. Table and userdata are generally dereferenced to access individual members, so that behaves like pass-by-reference for common usage.

----------------------------------------------------------------

PS: Strings are implemented a bit like that already in most languages. See http://en.wikipedia.org/wiki/String_interning

Last edited by Sasky : 12/17/14 at 02:43 PM.
  Reply With Quote