Lua Faq翻译之为什么lua中没有类似于+=的操作符以及用Lua实现C++中的<<操作符
来源:互联网 发布:艾媒咨询数据 编辑:程序博客网 时间:2024/06/01 10:00
链接:http://lua-users.org/wiki/LuaFaq和http://lua-users.org/wiki/CustomOperators
在实现lua时,目标之一就是简洁。大多数语言包括许多特性,这就意味着他们有许多复杂的特效,比如C++,pythoh,Lisp。很少语言像Forth和lua这样,语法简洁。lua目标是提供那些真正需要的原子特性,但如果需要,你可以利用这些原子特性,实现许多其他复杂的特性。比如可以实现其他语言中的模块(modules),面向对象(OO),在lua 5以后的版本中,还可以利用协程(coroutines)实现异常(exceptions)和线程(threads)。因此lua中没有类似于+=操作符。
如果你真正想实现类似的功能,可以参考链接:http://lua-users.org/wiki/CustomOperators,比如下面的代码,就实现了类似于C++中的<<操作符:
-----------------------------------
--用lua实现类似于c++中的输出操作符<<
--来自:http://lua-users.org/wiki/CustomOperators
-----------------------------------
local CustomOp = {}
function CustomOp:__div(b)
return getmetatable(self.a)["__" .. self.op](self.a,b)
end
setmetatable(CustomOp, {__call =
function(class,a,op)
return setmetatable({a = a,op = op},CustomOp) --注意函数setmetatable返回的是要设置的元表的table
end
})
function enable_cution_ops(mt)
function mt:__div(op)
return CustomOp(self,op)
end
end
osstream = {}
osstream.__index = osstream
enable_cution_ops(osstream)
function osstream:write(s)
io.write(s)
end
osstream['__<<'] = function(self,s)
self:write(s)
return self
end
setmetatable(osstream,{__call =
function(class,file)
file = file or io.output()
return setmetatable({file = file},osstream)
end
})
cout = osstream()
endl = "\n"
--例子
local _=cout / '<<' / 'hello' / '<<' / ',world!' / '<<' / endl --输出:hello,world!
在实现lua时,目标之一就是简洁。大多数语言包括许多特性,这就意味着他们有许多复杂的特效,比如C++,pythoh,Lisp。很少语言像Forth和lua这样,语法简洁。lua目标是提供那些真正需要的原子特性,但如果需要,你可以利用这些原子特性,实现许多其他复杂的特性。比如可以实现其他语言中的模块(modules),面向对象(OO),在lua 5以后的版本中,还可以利用协程(coroutines)实现异常(exceptions)和线程(threads)。因此lua中没有类似于+=操作符。
如果你真正想实现类似的功能,可以参考链接:http://lua-users.org/wiki/CustomOperators,比如下面的代码,就实现了类似于C++中的<<操作符:
-----------------------------------
--用lua实现类似于c++中的输出操作符<<
--来自:http://lua-users.org/wiki/CustomOperators
-----------------------------------
local CustomOp = {}
function CustomOp:__div(b)
return getmetatable(self.a)["__" .. self.op](self.a,b)
end
setmetatable(CustomOp, {__call =
function(class,a,op)
return setmetatable({a = a,op = op},CustomOp) --注意函数setmetatable返回的是要设置的元表的table
end
})
function enable_cution_ops(mt)
function mt:__div(op)
return CustomOp(self,op)
end
end
osstream = {}
osstream.__index = osstream
enable_cution_ops(osstream)
function osstream:write(s)
io.write(s)
end
osstream['__<<'] = function(self,s)
self:write(s)
return self
end
setmetatable(osstream,{__call =
function(class,file)
file = file or io.output()
return setmetatable({file = file},osstream)
end
})
cout = osstream()
endl = "\n"
--例子
local _=cout / '<<' / 'hello' / '<<' / ',world!' / '<<' / endl --输出:hello,world!
- Lua Faq翻译之为什么lua中没有类似于+=的操作符以及用Lua实现C++中的<<操作符
- 【Lua】Lua中的Table操作
- lua中操作符的优先级
- Lua 长度操作符
- Lua逻辑操作符
- lua中io的操作
- Lua中的字符串操作
- lua c 栈操作
- Lua 在C程序中进行Lua表操作
- 关于”#”操作符在lua中table的运用
- 关于”#”操作符在lua中table的运用
- lua 的一些语句和操作符
- lua基础【二】lua中关于字符串的操作总结
- Lua的string操作
- lua io的操作
- lua的select操作
- Lua的IO操作
- lua 的io操作
- python的线程处理模块——thread,threading
- Oracle数据库日常维护知识总结
- const参数,const返回值与const函数
- python的时间处理模块 time
- fortran中提取字符串中可见字符的索引
- Lua Faq翻译之为什么lua中没有类似于+=的操作符以及用Lua实现C++中的<<操作符
- 题1021、1022、1023
- 年度网络购物十大被投诉网站淘宝、当当位居前二正文
- Java虚拟机类加载顺序研究
- Struts 2工作流程
- Java read and write csv file(注意事项)
- curl 命令使用
- Spring framework内容整理
- 从零开始学C++之模板(四):用模板实现单例模式(线程安全)、模板方式实现动态创建对象