Dot or Colon separator between table name and member in Lua - what is the difference?
Archive - Originally posted on "The Horse's Mouth" - 2014-05-06 19:07:36 - Graham Ellis
When do I use a dot and when do I use a colon in referencing a table member in Lua? It's a good question and one that we answer (and demonstrate to help make clear) on our Lua Programming Course.
Dot refers to a table member. So if I write source.read()
I am saying "run the read function in the table called source". Perhaps that's all I want to do, but it's more likely that the read funiction needs to refer to other things in that same table, so it needs to be passed in as a parameter - so source.read(source)
and that works perfectly well. But it looks clumsy, and it means the table name has to be entered twice into the code. So that can be rewritten as source:read()