Draft:Luau (programming language)

Luau
Screenshot of Lua code from a Wikipedia Lua module using the MediaWiki Scribunto extension
ParadigmMulti-paradigm: scripting, imperative (procedural, prototype-based, object-oriented), functional, meta, reflective
Designed byRoblox Corp
First appeared2019; 5 years ago (2019)
Typing disciplineDynamic, weak, duck
OSCross-platform
LicenseMIT License
Filename extensions.luau
Websitehttps://luau-lang.org/
Major implementations
Roblox Studio
Influenced by
Lua

Luau (lowercase u, /ˈlu.aʊ/) is a fast, small, safe, gradually typed embeddable scripting language derived from Lua. Lua is backwards-compatible with Lua 5.1 and is designed mainly for the popular video game platform Roblox.[1]

It was developed by Roblox's internal team along with open source contributors. It underpins all user-generated content on Roblox, offering APIs for 3D world manipulation, backend access, UI interaction, and more. Used by hundreds of thousands of developers, Luau is the first programming language for many and essential in Roblox Studio for creating plugins and games.

History

edit

In 2006, Roblox chose Lua as its programming language for users due to its simplicity, efficiency, and suitability for embedding in games, allowing users to script immersive games with ease. However, limitations in type checking, performance, and missing features in Lua 5.1 led to the development of Luau in 2019. Luau maintains backward compatibility with Lua 5.1 but introduces enhancements like gradual typing for large codebases, better performance, and additional features while keeping the language simple and beginner-friendly.

Luau's evolution is guided by a collaborative process involving compiler, runtime, type checker, and tooling development. This ensures efficient execution and practicality, with an emphasis on performance and memory optimization. Future developments will focus on enhancing type inference, exploring native code generation, and innovating in hardware utilization across cores and nodes. Luau continues to be refined to balance simplicity with advanced functionality, supporting the vast and diverse Roblox developer community.

Syntax

edit

The syntax of Luau is similar to Lua 5.1 but also include the following features:

String literals
edit
  • \xAB inserts a character with the code 0xAB into the string
  • \u{ABC} inserts a UTF8 byte sequence that encodes U+0ABC character into the string (note that braces are mandatory)
  • \z at the end of the line inside a string literal ignores all following whitespace including newlines, which can be helpful for breaking long literals into multiple lines.
Number literals
edit
  • Hexadecimal integer literals, 0xABC or 0XABC
  • Binary integer literals, 0b01010101 or 0B01010101
  • Decimal separators in all integer literals, using _ for readability: 1_048_576, 0xFFFF_FFFF, 0b_0101_0101
Continue statement
edit
if x < 0 then
    continue -- Not a keyword for backwards compatibility
end
Compound assingments
edit

Luau supports compound assignments with the following operators: +=, -=, *=, /=, //=, %=, ^=, ..=. Just like regular assignments, compound assignments are statements, not expressions:

-- this works
a += 1

-- this doesn't work
print(a += 1)

-- calls foo() twice
a[foo()] = a[foo()] + 1

-- calls foo() once
a[foo()] += 1
Type annotations
edit
-- Type annotations can be declared for local variables, function arguments and function return types using : as a separator
function foo(x: number, y: string): boolean
    local k: string = y:rep(x)
    return k == "a"
end

-- Function types are specified using the arguments and return types, separated with ->:
local foo: (number, string) -> boolean

-- To return no values or more than one, you need to wrap the return type position with parentheses, and then list your types there.
local no_returns: (number, string) -> ()
local returns_boolean_and_string: (number, string) -> (boolean, string)

function foo(x: number, y: number): (number, string)
    return x + y, tostring(x) .. tostring(y)
end

-- Table types are specified using the table literal syntax, using : to separate keys from values
local array: { [number] : string }
local object: { x: number, y: string }

-- Declaring type aliases
type Point = { x: number, y: number }
type Array<T> = { [number]: T }
type Something = typeof(string.gmatch("", "%d"))
If-then-else Statements
edit
-- Simple Example
local maxValue = if a > b then a else b

-- Using elseif
local sign = if x < 0 then -1 elseif x > 0 then 1 else 0
Generalised Iteration
edit
-- No need for next keyword, or pairs() or ipairs()
for k, v in {1, 4, 9} do
    assert(k * k == v)
end
String interpolation
edit
-- Use backticks to format strings
local count = 3
print(`Bob has {count} apple(s)!`)
-- Bob has 3 apple(s)!

print`hello` -- Invalid
print "Hello" -- Valid
print(`hello`) -- Valid

References

edit
  1. ^ https://luau-lang.org/ Luau's website.