Packages and Modules
Namespace Types
A type can group related helpers so callers use a clear prefix.
Group helpers under a type
namespace_types.swift
Replay: real traced execution (multi-file project)
enum GeometryKit {
static func rectangleArea(width: Int, height: Int) -> Int {
return width * height
}
}
let width = 8
let height = 3
let area = GeometryKit.rectangleArea(width: width, height: height)
let label = "area=\(area)"
print(label)
enum GeometryKit {
static func rectangleArea(width: Int, height: Int) -> Int {
return width * height
}
}
let width = 5
let height = 3
let area = GeometryKit.rectangleArea(width: width, height: height)
let label = "area=\(area)"
print(label)
enum GeometryKit {
static func rectangleArea(width: Int, height: Int) -> Int {
return width * height
}
}
let width = 12
let height = 3
let area = GeometryKit.rectangleArea(width: width, height: height)
let label = "area=\(area)"
print(label)
width ← 8, height ← 3
7let width→ 8 = 8 //@width=5, 128let height→ 3 = 39let area = GeometryKit.rectangleArea(width: width8, height: height3)10let label = "area=\(area)"static func rectangleArea(width: Int, height: Int) -> Int
1enum GeometryKit {2 static func rectangleArea(width8: Int, height3: Int) -> Int {3 return width8 * height34 }area ← 24, label ← area=24
8let height = 39let area→ 24 = GeometryKit.rectangleArea(width: width8, height: height3)10let label→ area=24 = "area=\(area24)"1112print(labelarea=24)outputarea=24
width ← 5, height ← 3
7let width→ 5 = 58let height→ 3 = 39let area = GeometryKit.rectangleArea(width: width5, height: height3)10let label = "area=\(area)"static func rectangleArea(width: Int, height: Int) -> Int
1enum GeometryKit {2 static func rectangleArea(width5: Int, height3: Int) -> Int {3 return width5 * height34 }area ← 15, label ← area=15
8let height = 39let area→ 15 = GeometryKit.rectangleArea(width: width5, height: height3)10let label→ area=15 = "area=\(area15)"1112print(labelarea=15)outputarea=15
width ← 12, height ← 3
7let width→ 12 = 128let height→ 3 = 39let area = GeometryKit.rectangleArea(width: width12, height: height3)10let label = "area=\(area)"static func rectangleArea(width: Int, height: Int) -> Int
1enum GeometryKit {2 static func rectangleArea(width12: Int, height3: Int) -> Int {3 return width12 * height34 }area ← 36, label ← area=36
8let height = 39let area→ 36 = GeometryKit.rectangleArea(width: width12, height: height3)10let label→ area=36 = "area=\(area36)"1112print(labelarea=36)outputarea=36
namespace
Swift modules are namespaces, and small programs can also group related static helpers under an enum or struct name.