- OpenResty完全開發指南:構建百萬級別并發的Web應用
- 羅劍鋒
- 315字
- 2019-07-25 11:55:08
3.3 數據類型
Lua語言提供六種基本的數據類型:
■ nil :表示不存在的空對象或無效值,類似Python的None; ■ boolean :布爾類型,取值為true或false;
■ number :數字類型,不區分整數和浮點數;注
■ string :字符串類型,參見3.4節;
■ function :函數類型,參見3.8節;
■ table :表類型,非常靈活的數據結構,參見3.9節。
注:這是Lua 5.1和5.2的語法,Lua 5.3引入了整數類型,并支持了位運算。
使用函數type()可以測試變量的類型,它以字符串的形式返回類型的名字,例如:
print(type(nil)) -- nil print(type(true)) -- boolean print(type(42)) -- number print(type(2.718)) -- number print(type("metroid")) -- string print(type(print)) -- function(print是Lua標準庫里的一個函數) print(type(table)) -- table(table是Lua標準庫里的一個表)
雖然變量是有類型的,但因為Lua是動態語言,所以聲明變量并不需要顯式地寫出類型,變量也可以存儲任意類型的值:
x = 2018 -- 變量的類型是number x = "lua" -- 變量的類型變為string x = nil -- 變量的類型變為nil
推薦閱讀
- Python數據可視化:基于Bokeh的可視化繪圖
- 劍指JVM:虛擬機實踐與性能調優
- Mastering PHP Design Patterns
- Mastering ServiceNow(Second Edition)
- Scala程序員面試算法寶典
- Protocol-Oriented Programming with Swift
- Creating Stunning Dashboards with QlikView
- Natural Language Processing with Java and LingPipe Cookbook
- 計算機應用基礎案例教程
- 用案例學Java Web整合開發
- 網絡數據采集技術:Java網絡爬蟲實戰
- 網頁設計與制作
- 算法超簡單:趣味游戲帶你輕松入門與實踐
- Java面向對象程序設計教程
- Swift Essentials(Second Edition)