I can’t figure out the necessity of using let
when checking for cases in a method (e.g. area()
). I thought if it would be “Value binding” in a switch statement, however enum cases are not used within the case body, and why create a constant of enum case? Obviously the code won’t work without let
, but I don’t understand why? The example ShapeDimensions
:
enum ShapeDimensions {
// point has no associated value - it is dimensionless
case point
// square's associated value is the length of one side
case square(side: Double)
// rectangle's associted value defines its width and height
case rectangle(width: Double, height: Double)
func area() -> Double {
switch self {
case .point:
return 0
case let .square(side: side):
return side * side
case let .rectangle(width: w, height: h):
return w * h
}
}
}
var squareShape = ShapeDimensions.square(side: 10.0)
var rectShape = ShapeDimensions.rectangle(width: 5.0, height: 10.0)
var pointShape = ShapeDimensions.point