lua: Learning Official Doc notes

本文深入探讨Lua中的动态类型变量,包括基本类型如nil、布尔值、数字、字符串等,并介绍了Lua中函数、用户数据、线程及表的特性和用法。此外,还详细解析了元表的工作原理及其如何影响表的行为。

dynamically typed vars:

basic types: nil, boolean, number, string, function, userdata, thread & table.

where nil has only one value, is mainly to differ from other types. absense of useful info

bool: false & true. so false & nil are both make a conditional false

number: int & floating point, 2 subtypes, rules to when to use which. also converting automatically, standard lua use 64-bit num. but can re-compile lua to use 32bit. (see luaconf.h

string: immutable sequence of bytes, containing all info in 8-bit value. encoding-agnostic (nice !

 

calling functions written in lua/c. 

 

userdata: store any c data. raw memory. 2 kinds of userdata: full userdata: object; light userdata: pointer value. this type of data has no predefined ops, only to assign or identity test. It cannot be created or modifiied in lua. only through C api. guarentees the integrity. But using metatables, user can define ops for full userdata.

 

thread: represents independent threads of execution and used to implement coroutines. its not same as os's threads... coroutines are supported on all systems. even on no thread systems..

 

table: assoc. arrays. map? yes. with tables, arrays can be indexed through values other than numbers, but not nil or NaN. where NaN is actually number typed and to represent INF. but the key value can be anything except nil.. pathetic.. and conversely.. very odd.. any key not belonging to a table has an associated value nil. a.name is equal to a["name"]. -- syntax sugar

 

sequence 

 

concept: equal without metamethods.. a.i, a.j refer to the same obj iff. i, j EWM. however, a.2 and a.2.0 refer to the same obj.. because float value which is actually a int will be automatically convert to the int.. so a.2.0 is actually a.2 .. table fields can be of any type.. including functions, tables can carry methods

 

tables, functions, threads, full userdata values are objs and refer to the address where locates. functions manipulate these refs only return the refer to these values, not modifying

 

function: type(), this returns a string describing the type of value..

special thing in table is the metatable, its inside the original table, besides the key and value, as another table. __index, __newindex, __call, __tostring. Where __index influence the behavior when referencing data not inside the main table, __newindex influences assignments of the keys, __call influences behavior when using tablename as methods, like table(), __tostring influences when print(table).. behavior.

 

转载于:https://www.cnblogs.com/sansna/p/5827587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值