问题小记之 使用 nil 索引 Lua table

49 篇文章 1 订阅

本文简单介绍了使用 nil 索引 Lua table 的一些问题

使用 Lua 已经不少时间了,遇到 “table index is nil” 的错误也很多次了,久而久之自己便形成了 Lua table 索引不能为 nil 的概念.

但实际上,上述概念是不准确的,虽然下面的 Lua 代码确实会引起 “table index is nil” 的错误:

local t = {}
-- use nil as table index
t[nil] = 0

但是如果我们仅使用 nil 为索引进行 table 取值的话,实际上并不会引起错误,仅是返回 nil 罢了:

local t = {}
-- use nil as table index
-- v is nil
local v = t[nil]

网上简单搜索了一下,未找到直接相关的答案,那就直接看看 Lua 源码(5.3.5 版本)吧~

其实答案还是挺简单的:

luaH_get 函数(索引 table 取值会调用到这个函数)中对于索引为 nil 的取值直接返回了 nil (并没有报错):

// ltable.c

const TValue *luaH_get (Table *t, const TValue *key) {
  switch (ttype(key)) {
    case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key));
    case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
    
    // return nil when key is nil
    case LUA_TNIL: return luaO_nilobject;
    
    case LUA_TNUMFLT: {
      lua_Integer k;
      if (luaV_tointeger(key, &k, 0)) /* index is int? */
        return luaH_getint(t, k);  /* use specialized version */
      /* else... */
    }  /* FALLTHROUGH */
    default:
      return getgeneric(t, key);
  }
}

而在 luaH_newkey 函数中(索引 table 赋值会调用到这个函数),如果索引为 nil 则直接报错了:

// ltable.c

TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
  Node *mp;
  TValue aux;
  // error when key is nil
  if (ttisnil(key)) luaG_runerror(L, "table index is nil");
  
  // ...
}

不过从开发角度来讲,个人还是建议统一规避 index 为 nil 的情况,上面提及的 “Lua table 索引不能为 nil” 的概念虽然不准确,但作为开发准则的话却值得坚持~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值