首页 > 其他分享 >Lua 学习---2

Lua 学习---2

时间:2023-03-04 18:13:21浏览次数:42  
标签:学习 string userdata --- Lua values table type

值和类型

 

Lua is a dynamically typed language. This means that variables do not have types; only values do. There are no type definitions in the language. All values carry their own type.

All values in Lua are first-class values. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results.

There are eight basic types in Lua: nilbooleannumberstringfunctionuserdatathread, and tableNil is the type of the value nil, whose main property is to be different from any other value; it usually represents the absence of a useful value. Boolean is the type of the values false and true. Both nil and false make a condition false; any other value makes it true. Number represents real (double-precision floating-point) numbers. (It is easy to build Lua interpreters that use other internal representations for numbers, such as single-precision float or long integers; see file luaconf.h.) String represents arrays of characters. Lua is 8-bit clean: strings can contain any 8-bit character, including embedded zeros ('\0') (see §2.1).

Lua can call (and manipulate) functions written in Lua and functions written in C (see §2.5.8).

 

Lua 是动态类型的语言,可以不用定义,直接可以使用,类型根据值的实际类型选择。所有的值 都可以做为函数参数,也可以返回任何类型

在Lua中有8种数据类型  nil, boolean, number, string, function, userdata, thread, and table

 

 

 

The type userdata is provided to allow arbitrary C data to be stored in Lua variables. This type corresponds to a block of raw memory and has no pre-defined operations in Lua, except assignment and identity test. However, by using metatables, the programmer can define operations for userdata values (see §2.8). Userdata values cannot be created or modified in Lua, only through the C API. This guarantees the integrity of data owned by the host program.

userdata 是用来存储C语言数据结构的类型,这种类型的数据对应一块内存区域, userdata数据不能用Lua 创建或修改,但可以通过C API 操作。

 

 

The type table implements associative arrays, that is, arrays that can be indexed not only with numbers, but with any value (except nil). Tables can be heterogeneous; that is, they can contain values of all types (except nil). Tables are the sole data structuring mechanism in Lua; they can be used to represent ordinary arrays, symbol tables, sets, records, graphs, trees, etc. To represent records, Lua uses the field name as an index. The language supports this representation by providing a.name as syntactic sugar for a["name"]. There are several convenient ways to create tables in Lua (see §2.5.7).

Like indices, the value of a table field can be of any type (except nil). In particular, because functions are first-class values, table fields can contain functions. Thus tables can also carry methods (see §2.5.9).

table类型 Lua 中的表(table)其实是一个"关联数组"(associative arrays),数组的索引可以是数字、字符串或表类型。这种数组成员可以包含除nil之外 的所有类型。tables在Lua中可以表示 
普通的数组,符号表(键值对的集合,支持给定键查找值的操作),sets,records,graphs,tress 等,records在lua 中以字段名称为索引,可以通过 a.name 或 a["name"]使用。table的字段可以是
函数,在 Lua 里,table 的创建是通过"构造表达式"来完成,最简单构造表达式是{},用来创建一个空表。

 

Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.

 

Tables, functions, threads, and (full) userdata values 中的数据只是真实数据 的引用,不包含真实的数据 数据copy除外

 

Lua provides automatic conversion between string and number values at run time. Any arithmetic operation applied to a string tries to convert this string to a number, following the usual conversion rules. Conversely, whenever a number is used where a string is expected, the number is converted to a string, in a reasonable format. For complete control over how numbers are converted to strings, use the format function from the string library (see string.format). 

在字符串和数值两个类型之间存在着自动转换原则,具体的如果 是数学计算,优先 转字符串为数值;如果是字符串操作,默认按照特定的格式把数值 转为字符串(string.format)

 

未完待续 ....... 变量

 

标签:学习,string,userdata,---,Lua,values,table,type
From: https://www.cnblogs.com/hztech/p/17178722.html

相关文章

  • Java-类-对象 - 对象内存图 成员变量和局部变量的不同 this关键字 构造方法 封装
         packagecom.demo.css;publicclasscss01{//java要创造对象,必须要有类的存在//类:一组相关属性和行为的集合,看做是对象的设计图//对象......
  • The Missing Semester - 第五讲 学习笔记
    第五讲命令行环境课程视频地址:https://www.bilibili.com/video/BV1Dy4y1a7BW课程讲义地址:https://missing-semester-cn.github.io/2020/command-line/本机学习使用平......
  • 【Redis场景5】集群秒杀优化-分布式锁
    集群环境下的秒杀问题前序【Redis场景1】用户登录注册【Redis场景2】缓存更新策略(双写一致)【Redis场景3】缓存穿透、击穿问题【Redis场景拓展】秒杀问题-全局唯一ID......
  • Android学习-每日打卡APP-实现每日打卡
    继续写我的打卡APP-完成了每日打卡的功能,其实还是比较简单,因为和注册一样都是插入的过程同时还能实现自动计数的功能,把坚持天数自动计算出来,打卡后插入数据库效果,可以看......
  • Android-每日打卡APP-实现登录功能
    每日打卡APP新的进展-实现登录功能-昨天已经把注册功能实现了,今天也很快把登录功能做了出来,然后接着着手做其他功能,打卡功能写在下一篇博客能够实现登录和注册,注册相关的......
  • HTML--皮肤库
    <!doctypehtml><html><head><title>皮肤库</title></head><body><tablealign="center"><tr><td><divalign="center"><img......
  • 08:swift-枚举
      正文/*1:**Swift中的枚举则更加灵活,每一个枚举成员可以是字符串、字符、任意的整数值,或者是浮点类型。2:*一旦变量声明成枚举类型,你就可以用一个点语法......
  • Flutter 下载篇 - 叁 | 网络库切换实践与思考
    前言本文是关于使用flutter_download_manager下载功能的实践和探索。我们将基于flutter_download_manager的功能扩展,改造成自己想要的样子。在阅读本文之前,建议先了解前两......
  • 英语学习成长日记
    Firstrecord原文我学习英语有段时间了。突然意识到,可以写随笔记来记录一下我的学习。IhavebeenlearningEnglishforawhile.Suddenlyrealized,Icanwritea......
  • Html基础知识-标签
    一、HTML介绍HTML是一种标记语言,不是编程语言,为了快速上手,其他的可以自己查询,下面是我使用VScode生成的第一个HTML网页。二、HTML标签介绍标签分为单标签和双标签,<br>......