Type: concept
Confidence: 0.88
Created: 2026-04-16
Updated: 2026-04-16
Tags: 技术Lua编程语言游戏开发

Luau

概述

Luau 是 Roblox 对 Lua 5.1 的方言扩展,增加了渐进式可选静态类型系统、泛型、联合类型,并通过本地字节码编译和 Native codegen(实验性)提升运行时性能,同时维持与 Lua 5.1 的语法兼容。

关键内容

类型系统

Luau 引入可选静态类型注解,类型检查在编辑器阶段(Roblox Studio)运行,运行时仍为动态类型: - 基础注解local x: number = 42local name: stringlocal b: boolean - 可选类型local maybe: string? 等价于 string | nil - 函数类型local fn: (number, string) -> boolean - 联合/字面量类型type State = "idle" | "run" | "jump" | "dead" - 泛型type Array<T> = {[number]: T}type Dict<K, V> = {[K]: V};支持泛型函数 function first<T>(arr: {T}): T? - 交叉类型(组合)type NamedEntity = Named & Positioned - 类型断言local x = getValue() :: string - typeof 检测Robloxtypeof() 而非 type(),因 type(Vector3.new()) == "userdata"typeof(Vector3.new()) == "Vector3" - 嵌套结构体:支持多层嵌套 table 类型定义,如 PlayerDatastats: { kills: number, deaths: number }

性能特性

与标准 Lua 的关系

Luau 基于 Lua 5.1 扩展,语法层面向下兼容。主要新增:类型注解语法、continue 语句、bit32 库内建、CompileOptions 控制等。不支持 Lua 5.2+ 的 goto、5.4 的整数类型等特性。

应用范围

Luau 目前主要在 Roblox 平台使用,但 Luau 已开源(luau-lang.org),可作为独立 Lua 运行时嵌入其他引擎。其类型系统设计对 Lua 生态的演进有参考价值。

来源

相关