lua - 为什么调用 Lua 中的这个函数需要一个额外的参数?

标签 lua

在文件 linalg.lua 中我有这个函数声明:

function dot(A,B)
   return sum(mult(A,B),2); -- sum along second dimension
end

然后在另一个文件中我有这些调用:

require 'linalg'

-- First fundamental Coeffecients of the surface (E,F,G)
local E = dot(Xu,Xu,2)
local F = dot(Xu,Xv,2)
local G = dot(Xv,Xv,2)

local m = cross(Xu,Xv,2)
local p = sqrt( dot(m,m,2) )
local n = div(m,concath(p, p, p))

-- Second fundamental Coeffecients of the surface (L,M,N)
local L = dot(Xuu,n,2)
local M = dot(Xuv,n,2)
local N = dot(Xvv,n,2)

我不明白的是:

Why the dotfunction is called with three arguments (being 2 always the last of them) if the function is declared with two arguments? Is it some Lua idiom?

代码在给出正确结果的系统中运行良好,现在我的任务是将其转换为 Python/Numpy。

最佳答案

引自 http://www.lua.org/pil/5.html

Parameters work exactly as local variables, initialized with the actual arguments given in the function call. You can call a function with a number of arguments different from its number of parameters. Lua adjusts the number of arguments to the number of parameters, as it does in a multiple assignment: Extra arguments are thrown away; extra parameters get nil.

所以简单地忽略了额外的参数,缺少的参数为零。是的。它是语言工作方式的一部分,非常适合使用。

关于lua - 为什么调用 Lua 中的这个函数需要一个额外的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23960404/

相关文章:

io - 如何在Lua中读取C生成的二进制文件

lua - 无法弄清楚lua表继承

json - 正文中带有 json 的原始 POST 请求

lua - 在lua中连接表序列

c++ - 在 C++ 中嵌入 Lua : Accessing C++ created through Lua, 回到 C++(或将结果从 Lua 返回到 C++)

file-io - 尝试让 Lua 中的程序从文件中的各个行读取信息

file - 将csv文件复制到lua中的新文件

ruby-on-rails - 用于 Lua Web 开发的类似 POW 的服务器( super 简单/无需配置)?

lua - 如何改进一长串 elseif 语句?

c - Lua 是否优化了 ".."运算符?