Lua学习笔记之Expressions
2017-07-04 19:33
169 查看
Both function calls and vararg expressions can result in multiple values. If an expression is used as a statement (only possible for function calls (see §2.4.6)),
then its return list is adjusted to zero elements, thus discarding all returned values. If an expression is used as the last (or the only) element of a list of expressions, then no adjustment is made (unless the call is enclosed in parentheses). In all other
contexts, Lua adjusts the result list to one element, discarding all values except the first one.
Here are some examples:
Any expression enclosed in parentheses always results in only one value. Thus,
the first value returned by
then its return list is adjusted to zero elements, thus discarding all returned values. If an expression is used as the last (or the only) element of a list of expressions, then no adjustment is made (unless the call is enclosed in parentheses). In all other
contexts, Lua adjusts the result list to one element, discarding all values except the first one.
Here are some examples:
f() -- adjusted to 0 results g(f(), x) -- f() is adjusted to 1 result g(x, f()) -- g gets x plus all results from f() a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) a,b = ... -- a gets the first vararg parameter, b gets -- the second (both a and b can get nil if there -- is no corresponding vararg parameter) a,b,c = x, f() -- f() is adjusted to 2 results a,b,c = f() -- f() is adjusted to 3 results return f() -- returns all results from f() return ... -- returns all received vararg parameters return x,y,f() -- returns x, y, and all results from f() {f()} -- creates a list with all results from f() {...} -- creates a list with all vararg parameters {f(), nil} -- f() is adjusted to 1 result
Any expression enclosed in parentheses always results in only one value. Thus,
(f(x,y,z))is always a single value, even if
freturns several values. (The value of
(f(x,y,z))is
the first value returned by
for nil if
fdoes not return any values.)
相关文章推荐
- LUA学习笔记2-Iterators and the Generic for
- [LUA学习笔记01]用VS2003(C++)编译Hello Lua程序
- Lua入门系列----pil学习笔记之Type and Values (2)
- Lua入门系列----pil学习笔记之Getting Start
- Lua入门系列----pil学习笔记之Type and Values (2)
- Lua入门系列----pil学习笔记之 Type and Values (1)
- Lua入门系列----pil学习笔记之Getting Start
- LUA学习笔记(1)
- Lua入门系列----pil学习笔记之Getting Start
- Lua学习笔记四--返回值和UserData
- LUA学习笔记1-Functions
- Lua学习笔记之 Type and Values
- LUA学习笔记3 - Compilation, Execution, and Errors
- Lua学习笔记五--真正的入门:编写Lua扩展库
- Lua学习笔记一
- [LUA学习笔记03]LUA函数和C函数的相互调用
- Lua 学习笔记之 function、closure和upvalue
- Lua入门系列----pil学习笔记之Type and Values (2)
- Lua入门系列----pil学习笔记之Type and Values (2)
- Lua入门系列----pil学习笔记之Getting Start