📄 manual.html
字号:
If the expression is used inside another expressionor in the middle of a list of expressions,then its result list is adjusted to one element,thus discarding all values except the first one.If the expression is used as the last element of a list of expressions,then no adjustment is made,unless the call is enclosed in parentheses.<p>Here are some examples:<pre> f() -- adjusted to 0 results g(f(), x) -- f() is adjusted to 1 result g(x, f()) -- g gets x plus all values returned by 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 may 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 values returned by f() return ... -- returns all received vararg parameters return x,y,f() -- returns x, y, and all values returned by f() {f()} -- creates a list with all values returned by f() {...} -- creates a list with all vararg parameters {f(), nil} -- f() is adjusted to 1 result</pre><p>An expression enclosed in parentheses always results in only one value.Thus,<code>(f(x,y,z))</code> is always a single value,even if <code>f</code> returns several values.(The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>or <b>nil</b> if <code>f</code> does not return any values.)<p><a name="arith"></a><a name="2.5.1"></a><h3>2.5.1 - Arithmetic Operators</h3>Lua supports the usual arithmetic operators:the binary <code>+</code> (addition),<code>-</code> (subtraction), <code>*</code> (multiplication),<code>/</code> (division), <code>%</code> (modulo), and <code>^</code> (exponentiation);and unary <code>-</code> (negation).If the operands are numbers, or strings that can be converted tonumbers (see <a href="#coercion">2.2.1</a>),then all operations have the usual meaning.Exponentiation works for any exponent.For instance, <code>x^(-0.5)</code> computes the inverse of the square root of <code>x</code>.Modulus is defined as<pre> a % b == a - math.floor(a/b)*b</pre>That is, it is the remainder of a division that roundsthe quotient towards minus infinity.<p><a name="rel-ops"></a><a name="2.5.2"></a><h3>2.5.2 - Relational Operators</h3>The relational operators in Lua are<pre> == ~= < > <= >=</pre>These operators always result in <b>false</b> or <b>true</b>.<p>Equality (<code>==</code>) first compares the type of its operands.If the types are different, then the result is <b>false</b>.Otherwise, the values of the operands are compared.Numbers and strings are compared in the usual way.Objects (tables, userdata, threads, and functions)are compared by <em>reference</em>:Two objects are considered equal only if they are the <em>same</em> object.Every time you create a new object(a table, userdata, thread, or function),this new object is different from any previously existing object.<p>You can change the way that Lua compares tables and userdata by using the "eq" metamethod (see <a href="#metatable">2.8</a>).<p>The conversion rules of <a href="#coercion">2.2.1</a><em>do not</em> apply to equality comparisons.Thus, <code>"0"==0</code> evaluates to <b>false</b>,and <code>t[0]</code> and <code>t["0"]</code> denote differententries in a table.<p>The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).<p>The order operators work as follows.If both arguments are numbers, then they are compared as such.Otherwise, if both arguments are strings,then their values are compared according to the current locale.Otherwise, Lua tries to call the "lt" or the "le"metamethod (see <a href="#metatable">2.8</a>).<p><a name="logic"></a><a name="2.5.3"></a><h3>2.5.3 - Logical Operators</h3>The logical operators in Lua are<pre> and or not</pre>Like the control structures (see <a href="#control">2.4.4</a>),all logical operators consider both <b>false</b> and <b>nil</b> as falseand anything else as true.<p>The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.The conjunction operator <b>and</b> returns its first argumentif this value is <b>false</b> or <b>nil</b>;otherwise, <b>and</b> returns its second argument.The disjunction operator <b>or</b> returns its first argumentif this value is different from <b>nil</b> and <b>false</b>;otherwise, <b>or</b> returns its second argument.Both <b>and</b> and <b>or</b> use short-cut evaluation;that is,the second operand is evaluated only if necessary.Here are some examples:<pre> 10 or 20 --> 10 10 or error() --> 10 nil or "a" --> "a" nil and 10 --> nil false and error() --> false false and nil --> false false or nil --> nil 10 and 20 --> 20</pre>(In this manual,`<code>--></code>´ indicates the result of the preceding expression.)<p><a name="concat"></a><a name="2.5.4"></a><h3>2.5.4 - Concatenation</h3>The string concatenation operator in Lua isdenoted by two dots (`<code>..</code>´).If both operands are strings or numbers, then they are converted tostrings according to the rules mentioned in <a href="#coercion">2.2.1</a>.Otherwise, the "concat" metamethod is called (see <a href="#metatable">2.8</a>).<p><a name="len-op"></a><a name="2.5.5"></a><h3>2.5.5 - The Length Operator</h3><p>The length operator is denoted by the unary operator <code>#</code>.The length of a string is its number of bytes(that is, the usual meaning of string length when eachcharacter is one byte).<p>The length of a table <code>t</code> is defined to be anyinteger index <code>n</code>such that <code>t[n]</code> is not <b>nil</b> and <code>t[n+1]</code> is <b>nil</b>;moreover, if <code>t[1]</code> is <b>nil</b>, <code>n</code> may be zero.For a regular array, with non-nil values from 1 to a given <code>n</code>,its length is exactly that <code>n</code>,the index of its last value.If the array has "holes"(that is, <b>nil</b> values between other non-nil values),then <code>#t</code> may be any of the indices thatdirectly precedes a <b>nil</b> value(that is, it may consider any such <b>nil</b> value as the end ofthe array). <p><a name="2.5.6"></a><h3>2.5.6 - Precedence</h3>Operator precedence in Lua follows the table below,from lower to higher priority:<pre> or and < > <= >= ~= == .. + - * / % not # - (unary) ^</pre>As usual,you can use parentheses to change the precedences of an expression.The concatenation (`<code>..</code>´) and exponentiation (`<code>^</code>´)operators are right associative.All other binary operators are left associative.<p><a name="tableconstructor"></a><a name="2.5.7"></a><h3>2.5.7 - Table Constructors</h3>Table constructors are expressions that create tables.Every time a constructor is evaluated, a new table is created.Constructors can be used to create empty tables,or to create a table and initialize some of its fields.The general syntax for constructors is<pre> tableconstructor ::= `<b>{</b>´ [fieldlist] `<b>}</b>´ fieldlist ::= field {fieldsep field} [fieldsep] field ::= `<b>[</b>´ exp `<b>]</b>´ `<b>=</b>´ exp | Name `<b>=</b>´ exp | exp fieldsep ::= `<b>,</b>´ | `<b>;</b>´</pre><p>Each field of the form <code>[exp1] = exp2</code> adds to the new table an entrywith key <code>exp1</code> and value <code>exp2</code>.A field of the form <code>name = exp</code> is equivalent to<code>["name"] = exp</code>.Finally, fields of the form <code>exp</code> are equivalent to<code>[i] = exp</code>, where <code>i</code> are consecutive numerical integers,starting with 1.Fields in the other formats do not affect this counting.For example,<pre> a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }</pre>is equivalent to<pre> do local t = {} t[f(1)] = g t[1] = "x" -- 1st exp t[2] = "y" -- 2nd exp t.x = 1 -- t["x"] = 1 t[3] = f(x) -- 3rd exp t[30] = 23 t[4] = 45 -- 4th exp a = t end</pre><p>If the last field in the list has the form <code>exp</code>and the expression is a function call or a vararg expression,then all values returned by this expression enter the list consecutively(see <a href="#functioncall">2.5.8</a>).To avoid this,enclose the function call (or the vararg expression)in parentheses (see <a href="#expressions">2.5</a>).<p>The field list may have an optional trailing separator,as a convenience for machine-generated code.<p><a name="functioncall"></a><a name="2.5.8"></a><h3>2.5.8 - Function Calls</h3>A function call in Lua has the following syntax:<pre> functioncall ::= prefixexp args</pre>In a function call,first <em>prefixexp</em> and <em>args</em> are evaluated.If the value of <em>prefixexp</em> has type <em>function</em>,then this function is calledwith the given arguments.Otherwise, the <em>prefixexp</em> "call" metamethod is called,having as first parameter the value of <em>prefixexp</em>,followed by the original call arguments(see <a href="#metatable">2.8</a>).<p>The form<pre> functioncall ::= prefixexp `<b>:</b>´ Name args</pre>can be used to call "methods".A call <code>v:name(...)</code>is syntactic sugar for <code>v.name(v,...)</code>,except that <code>v</code> is evaluated only once.<p>Arguments have the following syntax:<pre> args ::= `<b>(</b>´ [explist1] `<b>)</b>´ args ::= tableconstructor args ::= String</pre>All argument expressions are evaluated before the call.A call of the form <code>f{...}</code> is syntactic sugar for <code>f({...})</code>;that is, the argument list is a single new table.A call of the form <code>f'...'</code>(or <code>f"..."</code> or <code>f[[...]]</code>) is syntactic sugar for <code>f('...')</code>;that is, the argument list is a single literal string.<p>As an exception to the free-format syntax of Lua,you cannot put a line break before the `<code>(</code>´ in a function call.This restriction avoids some ambiguities in the language.If you write<pre> a = f (g).x(a)</pre>Lua would see that as a single statement, <code>a = f(g).x(a)</code>.So, if you want two statements, you must add a semi-colon between them.If you actually want to call <code>f</code>,you must remove the line break before <code>(g)</code>.<p>A call of the form <code>return</code> <em>functioncall</em> is calleda <em>tail call</em>.Lua implements <em>proper tail calls</em>(or <em>proper tail recursion</em>):In a tail call,the called function reuses the stack entry of the calling function.Therefore, there is no limit on the number of nested tail calls thata program can execute.However, a tail call erases any debug information about thecalling function.Note that a tail call only happens with a particular syntax,where the <b>return</b> has one single function call as argument;this syntax makes the calling function return exactlythe returns of the called function.So, none of the following examples are tail calls:<pre> return (f(x)) -- results adjusted to 1 return 2 * f(x) return x, f(x) -- additional results f(x); return -- results discarded return x or f(x) -- results adjusted to 1</pre><p><a name="func-def"></a><a name="2.5.9"></a><h3>2.5.9 - Function Definitions</h3><p>The syntax for function definition is<pre> function ::= <b>function</b> funcbody funcbody ::= `<b>(</b>´ [parlist1] `<b>)</b>´ block <b>end</b></pre><p>The following syntactic sugar simplifies function definitions:<pre> stat ::= <b>function</b> funcname funcbody stat ::= <b>local</b> <b>function</b> Name funcbody funcname ::= Name {`<b>.</b>´ Name} [`<b>:</b>´ Name]</pre>The statement<pre> function f () ... end</pre>translates to<pre> f = function () ... end</pre>The statement<pre> function t.a.b.c.f () ... end</pre>translates to<pre> t.a.b.c.f = function () ... end</pre>The statement<pre> local function f () ... end</pre>translates to<pre> local f; f = function () ... end</pre><em>not</em> this:<pre> local f = function () ... end</pre>(This only makes a difference when the body of the functioncontains references to <code>f</code>.)<p>A function definition is an executable expression,whose value has type <em>function</em>.When Lua pre-compiles a chunk,all its function bodies are pre-compiled too.Then, whenever Lua executes the function definition,the function is <em>instantiated</em> (or <em>closed</em>).This function instance (or <em>closure</em>)is the final value of the expression.Different instances of the same functionmay refer to different external local variablesand may have different environment tables.<p>Parameters act as local variables that areinitialized with the argument values:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -