lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Mon, Feb 20, 2017 at 3:36 PM, Soni L. <fakedme@gmail.com> wrote:
Of course, lua can only typecheck at runtime. But here's an example:

    local function f(x)
      --assert(type(x) == "function")
      return {dothing=x}
    end

    local v = f(nil)

    v:dothing()


One problem with this code is that 'x' does not necessarily have to be a function to be callable. It could be a table that has a metatable with a __call field, or a user value with the same. The question "can this value be called?" is not that simple to answer.

local callable_types = { function = true, table = true } -- etc

local function is_callable(x)
  return callable_types[type(x)] == true
end

--
--