在 lua 中实现函数的重载。注:好吧,lua中原来可以实现重载...
local function create()

	local arg_table = {}
local function dispatcher (...)
local tbl = arg_table
local n = select ("#",...)
local last_match
for i = 1,n do
local t = type(select(i,...))
local n = tbl[t]
last_match = tbl["..."] or last_match
if not n then
return last_match (...)
end
tbl = n
end
return (tbl["__end"] or tbl["..."])(...)
end
local function register(desc,func)
local tbl = arg_table
for _,v in ipairs(desc) do
if v=="..." then
assert(not tbl["..."])
tbl["..."] = func
return
end
 
local n = tbl[v]
if not n then
n = {}
tbl[v]=n
end
tbl = n
end
tbl["__end"] = func
end
return dispatcher, register, arg_table
end
 
local all={}
local function register(env,desc,name)
local func = desc[#desc]
assert(type(func)=="function")
desc[#desc] = nil
 
local func_table
if all[env] then
func_table = all[env]
else
func_table = {}
all[env] = func_table
end
 
if env[name] then
assert(func_table[name])
else
env[name],func_table[name] = create()
end
 
func_table[name](desc,func)
end
 
define = setmetatable({},{
__index = function (t,k)
local function reg (desc)
register(getfenv(2),desc,k)
end
t[k] = reg
return reg
end
})

下面试一下:
define.test {

	"number",
function(n)
print("number",n)
end
}
 
define.test {
"string",
"number",
function(s,n)
print("string number",s,n)
end
}
 
define.test {
"number",
"...",
function(n,...)
print("number ...",n,...)
end
}
 
define.test {
"...",
function(...)
print("default",...)
end
}
 
test(1)
test("hello",2)
test("hello","world")
test(1,"hello")

输出:
  number 1

string number   hello   2
default hello world
number ... 1 hello
备注摘自:云风个人控件-----http://blog.codingnow.com/cloud/LuaFunctionOverload

在 lua 中实现函数的重载的更多相关文章

  1. Lua中的函数

    [前言] Lua中的函数和C++中的函数的含义是一致的,Lua中的函数格式如下: function MyFunc(param) -- Do something end 在调用函数时,也需要将对应的参数 ...

  2. c++继承关系中成员函数的重载、重写、重定义之间的区别

    1.Override.Overload.Redefine Overload 重载只能发生在类内部,不能发生在子类和父类的继承中.具体来说,如果子类中有父类同名.同返回值类型,但是不同参数列表,这两个在 ...

  3. Lua中assert( )函数的使用

    当Lua遇到不期望的情况时就会抛出错误,比如:两个非数字进行相加:调用一个非函数的变量:访问表中不存在的值等.你也可以通过调用error函数显示的抛出错误,error的参数是要抛出的错误信息. ass ...

  4. Lua中调用函数使用点号和冒号的区别

    1.初学者最易混乱Top1——调用函数时用点号还是用冒号? 我们来看看下面的两句代码: mSprite.setPosition(, ); mSprite:setPosition(, ); 对于初次接触 ...

  5. C调用Lua中的函数解析table

    Passing Tables to Lua Functions A use case that happens often is the passing of tables to and from L ...

  6. 两个函数彻底理解Lua中的闭包

    本文通过两个函数彻底搞懂Lua中的闭包,相信看完这两个函数,应该能理解什么是Lua闭包.废话不多说,上 code: --[[************************************** ...

  7. Lua中的常用语句结构以及函数

     1.Lua中的常用语句结构介绍 --if 语句结构,如下实例: gTable = {} ] ] then ]) == gTable[] then ]) else print("unkown ...

  8. Lua中的元表与元方法

    [前言] 元表对应的英文是metatable,元方法是metamethod.我们都知道,在C++中,两个类是无法直接相加的,但是,如果你重载了“+”符号,就可以进行类的加法运算.在Lua中也有这个道理 ...

  9. Lua中的元表与元方法学习总结

    前言 元表对应的英文是metatable,元方法是metamethod.我们都知道,在C++中,两个类是无法直接相加的,但是,如果你重载了"+"符号,就可以进行类的加法运算.在Lu ...

随机推荐

  1. Azure Service Bus 中的身份验证方式 Shared Access Signature

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  2. 在Web应用中接入微信支付的流程之极简清晰版

    在Web应用中接入微信支付的流程之极简清晰版 背景: 在Web应用中接入微信支付,我以为只是调用几个API稍作调试即可. 没想到微信的API和官方文档里隐坑无数,致我抱着怀疑人生的心情悲愤踩遍了丫们布 ...

  3. Enable MFA for a user

    If you are root/admin account, in order to configure a virtual MFA device, you must have physical ac ...

  4. 【转】【WebDriver】不可编辑域和日历控件域的输入 javascript

    http://blog.csdn.net/fudax/article/details/8089404 今天用到日历控件,用第一个javascript执行后页面上的日期控件后,在html中可以看到生效日 ...

  5. Win7下Eclipse中文字体太小

    http://www.cnblogs.com/newdon318/archive/2012/03/23/2413340.html 最近新装了Win7,打开eclipse3.7中文字体很小,简直难以辨认 ...

  6. 网页播放器(jsp、js)

    jsp对控件显示 <%@ page language="java" import="java.util.*" pageEncoding="UTF ...

  7. springMVC中一个controller多个方法

    web.xml文件: <?xml version="1.0" encoding="UTF-8"?><web-app version=" ...

  8. C#中的常见集合类的比较

    一.非泛型集合与泛型集合 非泛型集合:Array.ArrayList.HashTable.Queue.Statck.SortedList 泛型集合:List.Dictionary.Queue.Stac ...

  9. node.js问题二

    看了Node.js开发指南发现routes和app.js分开的话要使用下面代码 app.use(express.router(routes)) 但是真正是使用上面代码会遇到无数的问题报错 找了资料才发 ...

  10. EF(Linq)框架使用过程中的小技巧汇总

    这篇博客总结本人在实际项目中遇到的一些关于EF或者Linq的问题,作为以后复习的笔记或者供后来人参考(遇到问题便更新). 目录 技巧1: DbFunctions.TruncateTime()的使用 技 ...