Lua

Lua MoonScript 使用注意事项

2017-11-16  本文已影响29人  Separes

最近上了一个Lua/MoonScript的项目,第一次接触该语言,踩了一些坑,特此记录以为前车之鉴.

table = {}
first = table[1]
count: (table={}) =>
    count = 0
    for key,value in pairs(table)
        count += 1
    count

Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop:

tuples = {
  {"hello", "world"}
  {"egg", "head"}
}

for {left, right} in *tuples
  print left, right

We know each element in the array table is a two item tuple, so we can unpack it directly in the names clause of the for statement using a destructure.

test: (table, ...) =>
    ngx.say(...)
table = {}
@test table, '+', '1', 's'

ngx.exit(ngx.HTTP_OK) 
-- 结果: +1s

Our main concern with "continue" is that there are several other control structures that (in our view) are more or less as important as "continue" and may even replace it. (E.g., break with labels [as in Java] or even a more generic goto.) "continue" does not seem more special than other control-structure mechanisms, except that it is present in more languages. (Perl actually has two "continue"statements, "next" and "redo". Both are useful.

不过确实可以用麻烦一点的方法变相实现这个功能:

for k, v in pairs data
    while true do
        if conditions
            break
    flag = '1'
    if flag == '1'
        result = '2'
    else
        result = '3'

    @var_dump result

此时result的输出结果为{},其type为nil,而应该写为:

    flag = '1'
    result = ''
    if flag == '1'
        result = '2'
    else
        result = '3'

    @var_dump result

顺便上一个经常用的到的Lua Math库:
http://blog.csdn.net/goodai007/article/details/8076141

上一篇 下一篇

猜你喜欢

热点阅读