重载函数

lua-users home
wiki

重载函数允许你调用一个函数,并根据参数类型分派到不同的实现。C++ 和 Java 在语言级别支持此功能;在 Lua 中,你可以在运行时自己实现。

function overloaded()
    local fns = {}
    local mt = {}
    
    local function oerror()
        return error("Invalid argument types to overloaded function")
    end
    
    function mt:__call(...)
        local arg = {...}
        local default = self.default
        
        local signature = {}
        for i,arg in ipairs {...} do
            signature[i] = type(arg)
        end
        
        signature = table.concat(signature, ",")
        
        return (fns[signature] or self.default)(...)
    end
    
    function mt:__index(key)
        local signature = {}
        local function __newindex(self, key, value)
            print(key, type(key), value, type(value))
            signature[#signature+1] = key
            fns[table.concat(signature, ",")] = value
            print("bind", table.concat(signature, ", "))
        end
        local function __index(self, key)
            print("I", key, type(key))
            signature[#signature+1] = key
            return setmetatable({}, { __index = __index, __newindex = __newindex })
        end
        return __index(self, key)
    end
    
    function mt:__newindex(key, value)
        fns[key] = value
    end
    
    return setmetatable({ default = oerror }, mt)
end

你可以像这样使用它

foo = overloaded()

-- if passed a number, return its square
function foo.number(n)
    return n^2
end

-- if passed a string, convert it to a number and call the numeric version
function foo.string(s)
    return foo(tonumber(s))
end

-- if passed a string _and_ a number, act like string.rep
foo.string.number = string.rep

-- if given anything else, act like print
foo.default = print

--- begin test code ---
foo(6)
=> 36
foo("4")
=> 16
foo("not a valid number")
=> error (attempt to perform arithmetic on a nil value)
foo("foo", 4)
=> foofoofoofoo
foo(true, false, {})
=> true    false   table: 0x12345678

最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2015 年 2 月 6 日上午 5:33 GMT (差异)