Search code examples
lua

Lua has no class but dot operator?


If Lua has no class, why does it have a dot operator?

Eg. In string.find, is string a class with static/class method find?


Solution

  • In your example, find is an entry in the table string

    It is syntactic sugar for string["find"]

    It could be defined like so:

    local string = {
      "find" = function()
        -- find stuff
      end
    }
    

    or

    local string = {}
    string["find"] = function()
      -- find stuff
    end
    

    or

    local string = {}
    string.find = function()
      -- find stuff
    end