zoukankan      html  css  js  c++  java
  • lua5.1 和 5.2 关于 sequence 的定义变化,对#table取值的影响

    引子

    环境 lua 5.2

      a = {}
      for i=1,2 do a[i] = i*3 end


      a[4] = 11;
      print(a[#a])


      ---print 11


    -----------------------------------


      a = {}
      for i=1,3 do a[i] = i*3 end


      a[5] = 11;
      print(a[#a])
      
      ----print 9

    ---------------------------------

    #a  第一个的值是4   第二个的值是3    

    依据我之前从书中看到的知识点来看的话   #a  第一个的值是2   第二个的值是3    才对啊  

    为啥呢  

    刚開始入门,源码部分就跳过吧,又没有合适的关键词搜索,仅仅能到大婶群里问了

    一会儿   大神来了   给了个牛逼的解释 

    大婶:

     5.1 和 5.2 关于 sequence 的定义有变化. 
     5.1 的
    2.5.5 – The Length Operator

    The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, 
    the usual meaning of string length when each character is one byte).

    The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; 
    moreover, if t[1] is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n,
     its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, 
    it may consider any such nil value as the end of the array).



     5.2 的
    3.4.6 – The Length Operator

    The length operator is denoted by the unary prefix operator #. The length of a string is its number of bytes (that is,
     the usual meaning of string length when each character is one byte).

    A program can modify the behavior of the length operator for any value but strings through the __len metamethod 
    (see §2.4).

    Unless a __len metamethod is given, the length of a table t is only defined if the table is a sequence, that is,
     the set of its positive numeric keys is equal to {1..n} for some integer n. In that case, n is its length. Note that a table like

         {10, 20, nil, 40}
    is not a sequence, because it has the key 4 but does not have the key 3.
     (So, there is no n such that the set {1..n} is equal to the set of positive numeric keys of that table.) Note, however, that non-numeric keys do not interfere with whether a table is a sequence.


    从语言角度讲, lua 5.1 定义了 # 对数组取长度的约束. 而 lua 5.2 不严格定义了,仅仅说假设有 nil 就不确定


    也就是说 lua 5.2 放宽了语言定义,同意实现更灵活
     
    我:
    非常高深啊   我得慢慢啃  


    --[[高潮部分,直达G点]]
    大婶:
    没什么高深的, lua 5.2 说,你有 nil 就别指望 #table 对




    dun悟了......................level   +1

  • 相关阅读:
    UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
    关于自定义导航条UIBarButtonItem偏移的问题
    iOS端使用二维码扫描(ZBarSDK)和生成(libqrencode)功能
    CocoaPods安装和使用教程
    IOS中NSUserDefaults的用法(轻量级本地数据存储) (转)
    UIPickerView简单选择器的基本使用
    [原]Unity手游之路<三> 基于Unity+Java的聊天室源码
    [原]UML建模语言进阶
    [原]Java多线程编程学习笔记之九:使用wait/notify/notifyAll实现线程间通信的几点说明
    [原]MySQL的表分区
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3995507.html
Copyright © 2011-2022 走看看