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
测试成功,宝宝困了,累了,睡觉觉去~