惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
美团技术团队
量子位
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
小众软件
小众软件
博客园 - 司徒正美
罗磊的独立博客
云风的 BLOG
云风的 BLOG
B
Blog RSS Feed
博客园 - 聂微东

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Variadic Arguments in Lua
2022-12-02 · via jdhao's digital space

In Lua, to pass variable number of arguments, we can use ... (three ellipses) syntax in function argument.

Lua tutorial here tells us to use arg variable to access variadic arguments passed to functions. According to discussions here, this only works for Lua 5.0 and has been removed in Lua 5.1. See also changelog here:

  • The vararg system changed from the pseudo-argument arg with a table with the extra arguments to the vararg expression. (See compile-time option LUA_COMPAT_VARARG in luaconf.h.)

Instead, we can emulate that using the following code:

To get the number of extra arguments, we can use select('#', ...):

function my_fun(a, b, ...)
  local n = select('#', ...)
  print('num of extra arg:', n)
end

Note that when first argument to select is a number, select() will return all variable arguments starting from that index.

local v1, v2, v3, v4 = select(1, 'ab', 'b', 'c')  -- v1: 'ab', v2: 'b', v3: 'c', v4: nil
local v1, v2, v3, v4 = select(2, 'ab', 'b', 'c')  -- v1: 'b', v2: 'c', v3: nil, v4: nil
local v1, v2, v3, v4 = select(3, 'ab', 'b', 'c')  -- v1: 'c', v2: nil, v3: nil, v4: nil
local v1, v2, v3, v4 = select(4, 'ab', 'b', 'c')  -- all these variable is nil

As another example, we can calculate the sum of all arguments:

local fun = function (a, b, ...)
  local sum = 0
  sum = sum + a
  sum = sum + b
  local vararg = {...}
  for _, v in pairs(vararg) do
    sum = sum + v
  end
  return sum
end

print(fun(1, 2, 3, 4))

References#