Swift for Luau Developers
Move from Luau to Swift through optionals, value semantics, Unicode, protocols and ARC. Complete 48 multi-file exercises, build a parcel desk in four stages and port two complete Luau programs.
Luau and Swift side by side
The same program in Luau and in Swift, from the lessons of this course.
Bindings and Mutation
Luau table variables hold references. A local declaration or an array type annotation does not freeze the table, and table.clear through an alias clears the same underlying object. Static annotations constrain checked operations, while table.freeze is a…
Luau1local pending: {string} = {"upload", "index"}2local alias = pending3table.clear(alias)4print(tostring(#pending) .. " " .. tostring(#alias))Swift1import Foundation2let pending = ["upload", "index"]3var copy = pending4copy.removeAll()5print(pending.count, copy.count)Strings and Interpolation
Luau backtick interpolation evaluates expressions inside braces. Strings remain byte sequences, and the prefix check below uses byte positions on an ASCII path. string.rep repeats a string; neither interpolation nor repetition changes the indexing unit of the…
Luau1local folder = "cache"2local path = `{folder}/image`3print(path:sub(1, 6) == "cache/")4print(string.rep(".", 3))Swift1import Foundation2let folder = "cache"3let path = "\(folder)/image"4print(path.hasPrefix("cache/"))5print(String(repeating: ".", count: 3))Switch and Patterns
Luau uses ordered if checks rather than a built-in switch or tuple-pattern syntax. This function handles the more specific origin case before the broader vertical case. Reversing those checks would classify the origin differently, so branch ordering is part…
Luau1local function axis(x: number, y: number): string2 if x == 0 and y == 0 then return "origin" end3 if x == 0 then return "vertical" end4 return "other"5end6print(axis(0, 9))Swift1import Foundation2func axis(_ x: Int, _ y: Int) -> String {3 switch (x, y) {4 case (0, 0): return "origin"5 case (0, _): return "vertical"6 default: return "other"7 }8}Functions and Labels
Luau parameters are positional. A typed options table can give a field a name such as prefix, but the caller still supplies a table value as one argument. The field contract can be checked statically; it is different from a language-level external argument…
Luau1local function mark(text: string, options: {prefix: string}): string2 return options.prefix .. text3end4print(mark("draft", {prefix = "!"}))Swift1import Foundation2func mark(_ text: String, with prefix: String) -> String { prefix + text }Dictionaries and Missing Keys
Luau uses table keys for map entries. Assigning nil removes cache, and pairs can enumerate the remaining keys. The # operator is a sequence-length operation, not a general count of string keys. A stored false remains an entry even though it is falsey; nil and…
Luau1local flags: {[string]: boolean} = {cache = true}2flags.cache = nil3print(flags.cache or false)4local count = 05for _ in pairs(flags) do count += 1 end6print(count)Swift1import Foundation2var flags = ["cache": true]3flags["cache"] = nil4print(flags["cache"] ?? false)5print(flags.count)Structs and Independent Copies
A Luau record type describes a table's fields for static checking but does not give it value semantics. First and second still identify one table, so flipping its enabled field through second is visible through first. Type shape and runtime identity are…
Luau1type Toggle = {enabled: boolean}2local first: Toggle = {enabled = false}3local second = first4second.enabled = not second.enabled5print(tostring(first.enabled) .. " " .. tostring(second.enabled))Swift1import Foundation2struct Toggle {3 var enabled: Bool4 mutating func flip() { enabled.toggle() }5}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsStarting Swift
5 lessonsText and Decisions
5 lessonsControl Flow
5 lessonsFunctions and Arrays
5 lessonsCollection Tools
5 lessonsValues and References
5 lessonsFailures and Abstraction
6 lessonsDesigning Types
6 lessonsParcel Desk Project
4 lessonsLuau Ports
2 lessonsFrequently asked questions
Who is Swift for Luau Developers for?
For people who already write Luau and want to learn Swift. Each lesson starts from Luau you know and shows how Swift does the same thing, so you skip the beginner basics.
Do I need to know Swift already?
No. The course starts with your first Swift program. You need Luau basics: variables, conditions, loops, functions and collections.
How many lessons does Swift for Luau Developers have?
Swift for Luau Developers contains 48 interactive lessons you can complete at your own pace.
What does Swift for Luau Developers cover?
The course is organized into chapters covering: Starting Swift, Text and Decisions, Control Flow, Functions and Arrays, and more.
Is Swift for Luau Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.