Junior — Middle
Is it possible to use enumerations in programming languages to store properties that are calculated at runtime?
sobes.tech AI
Answer from AI
In Swift (a programming language for iOS), enums can contain computed properties that are evaluated at runtime. This is possible because enums in Swift can have computed properties and methods, but cannot store mutable state.
Example:
enum Direction {
case north, south, east, west
var description: String {
switch self {
case .north: return "North"
case .south: return "South"
case .east: return "East"
case .west: return "West"
}
}
}
let dir = Direction.north
print(dir.description) // Outputs "North"
Thus, enums can have properties that are computed at runtime, but cannot store mutable data.