Lua学习总结(值与类型)

Lua学习总结(值与类型)

晓宝老师平时写c++多,现在写lua的时候发现没有类型定义的语法…
关键字:值 类型

  • 类型
    在Lua中你会发现没有类型定义的语法,每个值都带有其自身类型信息,类型如下:
    1.nil(空)
    2.boolean(布尔)
    3.number(数字)
    4.string(字符串)
    5.userdata(自定义类型)
    6.function(函数)
    7.thread(线程)
    8.table(表)
    用type函数测试一下:
local valNil = nil
local valBoolean = true
local valNumber = 1
local valString = "hello"
--[[userdata:
userdata用于表示应用程序或C语言库所创建的新类型。
由于userdata类型可以将任意的C语言数据(struct)存储到Lua变量中。
在Lua中,这种类型没有太多的预定义操作,只能进行赋值和相等性测试。
]]
local valFunc = function ()
    print "function"
end
local valTable = {a=1,b=2}
local valCo = coroutine.create(function ()
    print "coroutine"
    coroutine.yield(valCo)
end)
print(coroutine.status(valCo))     -- suspended
coroutine.resume(valCo)

if type(valNil) == "nil" then
    print "valNil is a nil"
end

if type(valBoolean) == "boolean" then
    print "valBoolean is a boolean"
end

if type(valNumber) == "number" then
    print "valNumber is a number"
end

if type(valString) == "string" then
    print "valString is a string"
end

if type(valFunc) == "function" then
    print "valFunc is a function"
end

if type(valTable) == "table" then
    print "valTable is a table"
end

if type(valCo) == "thread" then
    print "valCo is a thread"
end

测试成功,宝宝困了,累了,睡觉觉去~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值