读书笔记: Programming in Lua, 4th Edition.
HelloWorld
dofile 用来在交互式中加载lua文件。
lua 数据类型
There are eight basic types in Lua: nil, Boolean, number, string, userdata, function, thread, and table.
> type(nil) --> nil
> type(true) --> boolean
> type(10.4 * 3) --> number
> type("Hello world") --> string
> type(io.stdin) --> userdata
> type(print) --> function
> type(type) --> function
> type({}) --> table
> type(type(X)) --> string
Booleans
Conditional tests consider both the Boolean false and nil as false and anything else as true. In particular, Lua considers both zero and the empty string as true in conditional tests.
and, or, not 操作符
The result of the and operator is its first operand if that operand is false; otherwise, the result is its second operand.
The result of the or operator is its first operand if it is not false; otherwise, the result is its second operand
> 4 and 5 --> 5
> nil and 13 --> nil
> false and 13 --> false
> 0 or 5 --> 0
> false or "hi" --> "hi"
> nil or false --> false
The not operator always gives a Boolean value:
> not nil --> true
> not false --> true
> not 0 --> false
> not not 1 --> true
> not not nil --> false
A useful Lua idiom is x = x or v, which is equivalent to (if not x then x = v end).
Another useful idiom is ((a and b) or c) or simply (a and b or c) (given that and has a higher precedence than or). It is equivalent to the C expression a ? b : c.