zoukankan      html  css  js  c++  java
  • Lua 初学者随笔 一

    1. 关于return

    function test(a, b)
        print("Hello")
        return 
        print("World")
    end
    
    --call the function
    test(1, 2);
    
    --[[output
    Hello 
    World
    ]]

    奇怪之处:

    ①Lua关于return语句放置的位置:return用来从函数返回结果,当一个函数自然结束结尾会的一个默认的return。Lua语法要求return只能出现在block的结尾一句(也就是说:任务chunk的最后一句,或者在end之前,或者else前,或until前),但是在本程序中竟然没有报错

    ②当执行return后,函数应该返回了,也就是说,上述程序本应该只输出Hello,然后函数就应当结束,不会再输出world,但是本程序确实Hello和world都输出了。

    ③若是return后面紧跟一个返回值,该程序就正常报错了,符合了Lua语法要求return只能出现在block的结尾一句。

    function test(a, b)
     print("Hello")
     return 1
     print("World")
    end
    
    --call the function
    test(1, 2);
    --[[error: 
    lua: hello.lua:4: 'end' expected (to close 'function' at line 1) near 'print'
    ]]

    2. 关于返回不定参数

    function newtable(...)
    return(...) -- error
    end
    tb1=newtable("a","v","c")
    for i=1,#tb1 do 
    print (i,tb1[i])
    end 

    return(...) 由于加了括号,只能返回第一个值,其它的值被忽略了
    正确的做法是:return{...}

    3.select函数

    function printargs(...)
    local num_args=select("#", ...)
        for i=1 ,num_args do 
            local arg=select(i, ...)
            print (i,arg)
       end
    end
    printargs("a","b",3,"d")

    select (index, ···)

    If index is a number, returns all arguments after argument number index. Otherwise, index must be the string "#", and select returns the total number of extra arguments it received.

    4.

  • 相关阅读:
    UITableView移除某行的分割线和让分割线宽度为cell的宽度
    UIButton防止被重复点击
    给View添加手势,防止点击View上其他视图触发点击效果
    自定义导航栏返回时的滑动手势处理
    一个UITableViewCell的简单动画效果
    二维码扫描
    代理的使用
    常用网站
    IOS 自定义View X系列出现一条线条
    UILabel自适应文本,让文本自适应
  • 原文地址:https://www.cnblogs.com/blankqdb/p/2966986.html
Copyright © 2011-2022 走看看