Generic functions & types
Write generic functions and types with type parameters (e.g., ). See how the compiler infers types and keeps code safe and reusable.
Generic functions & types is a free Swift Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why generics?
Generics let you write one function or type that works for many types while staying type-safe. You'll see <T> for type parameters and how inference makes calls simple.
Generic function
A generic function uses a type parameter (<T>). The compiler checks types for each call.
// Generic swap for any type T
func swapValues<T>(_ a: inout T, _ b: inout T) {
let temp = a
a = b
b = temp
}
var x = 1, y = 2
swapValues(&x, &y)
print(x, y) // 2 1
var s1 = "A", s2 = "B"
swapValues(&s1, &s2)
print(s1, s2) // B AGeneric type
Types can be generic too: declare Stack<Element> and reuse it for Int, String, and more.
// A minimal generic stack
struct Stack<Element> {
private var items: [Element] = []
mutating func push(_ x: Element) { items.append(x) }
mutating func pop() -> Element? { items.popLast() }
var top: Element? { items.last }
}
var intStack = Stack<Int>()
intStack.push(10); intStack.push(20)
print(intStack.top ?? -1) // 20
var textStack = Stack<String>()
textStack.push("hi"); textStack.push("bye")
print(textStack.pop() ?? "?")Type inference
Swift often infers generic types, so you rarely specify them explicitly at calls.
// The compiler infers T from arguments/assignments
func makePair<T>(_ a: T, _ b: T) -> (T, T) { (a, b) }
let pair1 = makePair(1, 2) // T inferred as Int
let pair2 = makePair("a", "b") // T inferred as String
print(pair1, pair2)Avoid duplication
Without generics, you repeat code for each type. Generics remove duplication while keeping static checks.
// Non-generic overloads would duplicate logic:
func eqInt(_ a: Int, _ b: Int) -> Bool { a == b }
func eqStr(_ a: String, _ b: String) -> Bool { a == b }
// A single generic can replace both:Notes & naming
Notes:
- Use clear names: T, Element, or domain-specific.
- Keep APIs small and focused.
- Next: add constraints with where to limit T when needed.
Meaning of <T>
Quick check: What does <T> mean in a generic function?
Recap
Recap: Define generic functions with <T> and build generic types like Stack<Element>. Swift infers types at calls, keeping code reusable and safe.
Frequently asked questions
Is the “Generic functions & types” lesson free?
Yes — the full text of “Generic functions & types” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Generic functions & types”?
Write generic functions and types with type parameters (e.g., ). See how the compiler infers types and keeps code safe and reusable. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Generic functions & types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Generic functions & types
- Constraints (where), type inference
- Generic algorithms on collections