Lua - Identifiers, Types, and Values
  1. What is Lua?
  2. Getting Started
  3. Identifiers, Types, and Values
  4. Variables and Expressions
  5. Operators
  6. Statements and Assignments
  7. Control Structures
  8. File I/O


Identifiers

Identifiers in Lua can be any string of letter, digits, and underscored, not beginning with a digit. This aligns with the identifier definition of most other languages. Some examples for valid identifier are sum, i, l21n, avg_marks, _mark, _MARK. However identifiers starting with an underscore followed by one or more uppercase letters should be avoided, as they are reserved for internal variables used in Lua.

The following are reserved and could not be used as identifiers in Lua:

andbreakdoelseelseif
endfalseforfunctionif
inlocalnilnotor
repeatreturnthentrueuntilwhile

Lua is case-sensitive, which means sum and SUM are two different identifiers.

Numerical constants may be written with an optional decimal part and an optional decimal exponent. Examples of valid numerical constants are 2, 3.0, 5.76, 0.576e1, 576E-2.

Types and Values

Lua is a dynamically typed language. The variables do not have a type associated with them, but the values do. The values carry their own types. The following types are the basic types in Lua:

nilRepresents the absence of a useful value. When a variable is not initialized, it is automatically set to nil in Lua. A value of nil makes a condition false.
booleanRepresents the types of true and false values. A value of false makes a condition false.
numberRepresents the real, double precision floating point, numbers.
stringRepresents an array of characters. Lua is 8 bit clean and strings may contain any 8-bit character. Strings can be delimited by double or single quotes.
functionFunctions are first class values in Lua. That means, functions can be stored in a variable, passed as argument to other functions and returned from a function.
userdataRepresents the block of raw memory. This type is provided to allow arbitrary C data to be stored in the Lua variables.
threadRepresents the independent threads of execution, used to implement the coroutines.
tableRepresents the associative arrays that can be indexed with any value, except nil. That is, you can index a table with a number or a string or any value except nil.

The type library function returns a string describing the type of a given value. The following chunk illustrates the type function:

print(type(a)) -- nil , as a is not assigned any value yet
print(type(nil)) -- nil
a=true
print(type(a)) -- boolean
a=10
print(type(a)) -- number
a="hello"
print(type(a)) -- string
print(type(print)) -– function

Marcadores: , , , , , , , , , , , , , , , , , ,





Links para esta postagem:

Criar um link



<< Início

Arquivos

Assinar Postagens [Atom]




waterlillies

waterlillies