概述
and use it like this:
hello.mytest.test2()
These problems can be avoided by not using the module
function but instead simply defining modules in the following simple way:
return M
and importing modules this way:
MT.test2()
A module containing a class with constructor (in the object-oriented sense) can be packaged in a number of ways in a module. Here is one reasonably good approach.[*2]
local M = {}; M.__index = M
local function construct()
local self = setmetatable({balance = 0}, M)
return self
function M:add(value) self.balance = self.balance + 1 end
return M
A module defined in this way typically only contains a single class (or at least a single public one), which is the module itself.
It can be used like this:
or even like this:
:
remove (filename (id))
end
Because "end
" is a terminator for many different constructs, it can help the reader (especially in a large block) if a comment is used to clarify which construct is being terminated: [*3]
if type(v) == "string" then
...lots of code here...
end -- if string
end -- for each t
if line then -- instead of line ~= nil
...
end
...
if not line then -- instead of line == nil
...
end
However, if the variable tested can ever contain false
as well, then you will need to be explicit if the two conditions must be differentiated: line == nil
v.s. line == false
.
and
and or
may be used for terser code:
x = x or "idunno"
-- rather than if x == false or x == nil then x = "idunno" end
print(x == "yes" and "YES!" or x)
-- rather than if x == "yes" then print("YES!") else print(x) end end
Clone a small table t
(warning: this has a system dependent limit on table size; it was just over 2000 on one system):
Determine if a table t
is empty (including non-integer keys, which #t
ignores):
t[#t+1] = 1更快速
t[#t+1] = 1
rather than
table.insert(t, 1)
.
最后
以上就是有魅力大侠为你收集整理的给用torch的童鞋,lua代码规范的全部内容,希望文章能够帮你解决给用torch的童鞋,lua代码规范所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复