官术网_书友最值得收藏!

Basic types

In the last section, you were introduced to the concepts of a variable and a value. This section explores the concept of what a value is. Every value has a data type, which intuitively describes what kind of data the value holds. Lua supports eight basic value types:

  • nil: The absence of data. This type represents literal nothingness. If a certain piece of data is invalid or unknown, nil is usually the best way to represent that it is invalid or unknown.
  • Boolean: A value of true or false. A Boolean value is binary and can only ever be in one of two states, true or false.
  • numberA number can represent any real number: 0, -1, 5, or even decimals such as 3.14159265359.
  • string: A string is an array of characters. When declaring a string literal, it must be "enclosed within quotation marks."
  • functionA function is some code that is referred to by a name and can be executed any time.
  • table: A table contains information using key-value pairs. Tables will be covered in depth in Chapter 3, Tables and Objects.
  • userdataComplex data structures defined in the C programming language.
  • thread: Threads can be used to execute code in parallel. Instead of your code running one set of commands, it can run several sets of commands at the same time.

This section will explore the nil, Boolean, and number types. The string and function types will get their own sections in this chapter. The table type is so important it will have its own chapter.

Lua uses loose, implicit types. That means a variable can have any type. Once a variable is assigned a type, it can be assigned any other type. For example, it is valid to assign a number to a variable that holds a string. After the assignment, the variable will simply hold a number.
主站蜘蛛池模板: 云梦县| 莲花县| 青阳县| 庐江县| 泰兴市| 蒲城县| 昌都县| 海阳市| 阿拉善左旗| 莱阳市| 班玛县| 洪洞县| 威信县| 苏尼特左旗| 靖西县| 白沙| 囊谦县| 孝昌县| 南雄市| 丹棱县| 道真| 丰城市| 含山县| 定西市| 贵州省| 咸丰县| 莒南县| 大同市| 镇远县| 尚义县| 开封市| 建湖县| 外汇| 利辛县| 登封市| 大同县| 和田市| 伊通| 呼和浩特市| 长葛市| 合川市|