Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yeah. One of the best things I've encountered in lua is the <strong>anything as references</strong> property.</p> <p>There's nothing wrong with the way you are using your <code>key</code>s in the table.</p> <h3>From <a href="http://www.lua.org/pil/2.5.html" rel="noreferrer">Lua PiL</a></h3> <blockquote> <p>Tables in Lua are neither values nor variables; they are objects.You may think of a table as a dynamically allocated object; your program only manipulates references (or pointers) to them. There are no hidden copies or creation of new tables behind the scenes.</p> </blockquote> <p>In your example, you haven't passed any argument to the function, so basically, it'll be useless in your case to have functions as reference in the program. On the other hand, something like this:</p> <pre><code>fn1 = function(x) print(x) end fn2 = function(x) print("bar") end t[fn1] = "foo" t[fn2] = "foo" for i, v in pairs(t) do i(v) end </code></pre> <p>does have its uses.</p> <blockquote> <blockquote> <p>Can Lua reuse function references once they are out of scope?</p> </blockquote> </blockquote> <p>As long as your parent table is in scope, yes. Since tables are created and manipulated but not copied, so there isn't a chance that your function reference can be deprecated from the table index memory. <em><strong>I'll edit this answer later after trying it out actually too.</strong></em></p> <blockquote> <blockquote> <p>Can this create any issues or is it considered a bad practice due to some reason?</p> </blockquote> </blockquote> <p>It's just considered a bad practise because users familiar with other languages, like <code>C</code>, <code>python</code> etc. tend to have array in mind when reading tables. In <a href="/questions/tagged/lua" class="post-tag" title="show questions tagged &#39;lua&#39;" rel="tag">lua</a> you have no such worries, and the program will work perfect.</p> <blockquote> <blockquote> <p>I don't know if I can rely on the uniqueness of the function references.</p> </blockquote> </blockquote> <p>Why?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload