Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 25 Apr 2012 07:10 PM |
Discuss: Weak Tables and their Application How can weak tables be applied, and what is the use of weak tables. Do you ever use weak tables, and if so, how? Are weak tables or setting a value to nil in a table a better way to clear it? And what is more advance in Lua then Weak Tables.
|
|
|
| Report Abuse |
|
|
iNigerian
|
  |
| Joined: 17 Apr 2012 |
| Total Posts: 165 |
|
|
| 25 Apr 2012 08:37 PM |
on the wiki it says its recommended to use garbage collector
isnt that locked on rbx.lua? |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 25 Apr 2012 10:44 PM |
"Lua then Weak Tables."
I win |
|
|
| Report Abuse |
|
|
|
| 25 Apr 2012 11:01 PM |
Looks like you don't understand exactly how they work...
I've found many occasions to use weak tables.
Usually, you will find an use of them when you're using a table that contains a list of something.
For example, if you make a list of all the events related to a certain GUI so that you can disconnect them when getting rid of the GUI (even though that's useless now that we have the Destroy method which disconnects them for us...). You could make that list be a weak table, so the connections could get garbage collected even if they still had a reference to them, as long as the only reference was the one in that table.
Another example:
Suppose you have a function that takes as an argument many objects. It then adds all these objects in a table and changes the name of all of them to a random number after 5 minutes (don't ask me why a such function would be useful, idk, it's just an example).
If there is no reference to one of these objects except the one in that table, then the object becomes useless and it doesn't matter anymore whether its name is changed or not since nothing will ever be able to know about the change anyway, right?
Therefore, you wouldn't care if the object just got garbage collected immediately. However, the reference to the object in the table prevents it from being garbage collected. That is, unless you use a weak table so it can still get garbage collected. |
|
|
| Report Abuse |
|
|
|
| 25 Apr 2012 11:02 PM |
*sigh*
The book Programming in Lua explains weak tables way better than me...
I've also found this nice document by Roberto Ierusalimschy which goes _VERY_ in depths into weak tables. |
|
|
| Report Abuse |
|
|
Legend26
|
  |
| Joined: 08 Sep 2008 |
| Total Posts: 10586 |
|
|
| 25 Apr 2012 11:10 PM |
| A registry of some sort could use weak tables so that the data in them does not persist once its not used anywhere else. |
|
|
| Report Abuse |
|
|
|
| 25 Apr 2012 11:14 PM |
That's a nice example, Legend26. Better than the two examples I could find... :P
If any of you is really interested by weak tables, or if you're NecroBumpist or Stravant, this _WILL_ interest you:
www dot inf dot puc-rio dot br slash ~roberto slash docs slash ry08-06 dot pdf |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 26 Apr 2012 12:12 AM |
The one thing that I end up using them for over and over again is "holding" a resource such as a spawn location, or origin for a specific player's 3D UI components to be placed relative to. In those cases the GC time does not matter, since there is a very large but still finite supply of resources that can be held, that is, a large enough amount that only a very very small fraction of them will be held at once, but a small enough amount that they would run out within the uptime of a normal server, so I have to reuse them at some point.
Sample "Origin Provider" class form one of my things:
class'OriginProvider'(nil, function(def) local mOriginList = setmetatable({}, {__mode='v'})
function def:requestOrigin() for i = 1, 100 do if not mOriginList[i] then local origin = Vector3.new(1000*i, 0, 0) mOriginList[i] = origin return origin end end end end)
For this to work the code that calls on it just has to hold a reference to the Vector3 origin that is returned as long as it still needs to have that origin to itself, and once it stops holding references to it, it automatically becomes free for use again.
That's one of the gems that I developed a couple of years ago and have been using since then, that I have not seen used elsewhere. |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 26 Apr 2012 02:52 AM |
They can be useful for storing private properties of objects, since that way the private properties get discarded when the object itself does:
Thingy = {} Thingy.__index = Thingy __private = setmetatable({}, {__mode = 'k'}) Thingy.new = function(p, q) local self = setmetatable({}, Thingy) __private[self] = {} self.property = p __private[self].privateProperty = q return self end |
|
|
| Report Abuse |
|
|