title: Lua: When is it possible to use colon syntax?
body: <p>While I understand the basic <a href="https://stackoverflow.com/q/4911186/1804173">difference between <code>.</code> and <code>:</code></a>, I haven't fully figured out when Lua allows to use the colon syntax. For instance, something like this does work:</p>
  
  <pre><code>s = "test"
  -- type(s) is string.
  -- so I can write a colon function for that type
  function string:myFunc() 
    return #self 
  end
  
  -- and colon function calls are possible
  s:myFunc()
  </code></pre>
  
  <p>However the same pattern does not seem to work for other types. For instance, when I have a <code>table</code> instead of a <code>string</code>:</p>
  
  <pre><code>t = {}
  -- type(t) is table.
  -- so I can write a colon function for that type
  function table:myFunc() 
    return #self 
  end
  
  -- Surprisingly, a colon function call is not not possible!
  t:myFunc() -- error: attempt to call method 'myFunc' (a nil value)
  -- But the verbose dot call works
  table.myFunc(t)
  </code></pre>
  
  <p>Moving on to another type:</p>
  
  <pre><code>x = 1
  -- type(x) is number.
  -- So I was expecting that I can write a colon function 
  -- for that type as well. However, in this case even this
  -- fails: 
  function number:myFunc() 
    return self 
  end
  -- error: attempt to index global 'number' (a nil value)
  </code></pre>
  
  <p>I'm currently trying to make sense of this. Is it correct to conclude that </p>
  
  <ul>
  <li>certain types like <code>string</code> allow both colon-function-definitions <em>and</em> colon-function-calls.</li>
  <li>other types like <code>table</code> allow only colon-function-definitions but not colon-function-calls.</li>
  <li>yet other types like <code>number</code> allow neither.</li>
  </ul>
  
  <p>What exactly is the reason for these differences? Is there a list of all types, showing which type of colon-syntax they support? Is there maybe a workaround for the <code>number</code> case, allowing to write e.g. <code>x:abs()</code>?</p>