Lua 中是否有类似于collections.defaultdictPython 中可用的功能,它自动处理不存在的关联数组键的默认值?
我希望下面的代码设置nil为v而不是错误。所以基本上是一种a[2](不存在的键)table默认情况下的方法:
a = {}
v = a[2][3]
>>> PANIC: unprotected error in call to Lua API (main.lua:603: attempt to index field '?' (a nil value))
在 Python 中可以这样完成:
>>> import collections
>>> a = collections.defaultdict(dict)
>>> print a[2]
{}