Lua Faq翻译之为什么lua中没有类似于+=的操作符以及用Lua实现C++中的<<操作符
2013-07-21 15:45
381 查看
链接: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!
相关文章推荐
- c++ 实现顺序栈类(初始化,入栈,退栈。读栈顶元素以及顺序输出栈顶指针与栈中的元素<<计算机软件技术基础 徐士良>>
- C++实现类String--含构造函数以及重载>>,<<,>,<,==,=
- <转>Class.forName的作用以及为什么要用它
- <C++ 实现设计模式> 观察者模式
- 《C++第九周实验报告1-1》--------接第8周任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出
- <C++ 实现设计模式> 观察者模式
- <C/C++>指向指针的指针、指向数组的指针以及存放指针的数组
- <实例>C++ 将一句话里的单词进行倒置,标点符号不倒换——使用string库实现
- 《C++第九周实验报告2-1》---接第8周任务2,定义Time类中的<<和>>运算符重载,实现时间的输入输出
- <leetcode系列> String to Integer (atoi) 以及atoi源码实现
- <谷歌如何测试> 翻译第六篇
- C++:模拟实现类似<time.h>的计时功能
- <<c++ primer>> 指向const对象指针&const指针
- <com原理>与<com内幕>的聚合实现部分
- 为什么使用<!DOCTYPE HTML>
- &lt;org manual&gt;翻译--安装
- &lt;org manual&gt;翻译--3.5.3 Emacs Lisp写成的公式
- &lt;org manual&gt;翻译--2.3 可见性的转换
- &lt;org manual&gt;翻译--4.4 链接的各种操作
- <frameset>使用注意以及页面输入乱码解决