Tsallaka zuwa abun ciki
Justice Tech School

Generics

Generics write code for any type with constraints.

Example
func firstMatch<T: Equatable>(_ a: [T], _ x: T) -> Int? { a.firstIndex(of: x) }
print(firstMatch([1, 2, 3], 3)!)
struct Box<T> { var value: T }
print(Box(value: "hi").value)

Exercise

Type parameter.

func f<>(x: T) {}

Quiz Test

What does T: Equatable require?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.