Recursive enumeration in Swift
Forums:
indirect enum Shape {
case Point(Int, Int)
case Line(Shape, Shape)
func toString() -> String {
switch self {
case .Point(let x, let y):
return "(\(x), \(y))"
case .Line(let p1, let p2):
return p1.toString() + "; " + p2.toString()
}
}
}
let point1 = Shape.Point(3, 4)
let point2 = Shape.Point(7, 9)
let line = Shape.Line(point1, point2)
let info = line.toString()
//info is "(3, 4); (7, 9)"
- Log in to post comments