Created
September 13, 2014 22:41
-
-
Save tjw/e1e3bac96f2b896e841c to your computer and use it in GitHub Desktop.
switch with enum case with struct associated value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
xcrun swiftc -c switch-enum-struct-case.swift | |
switch-enum-struct-case.swift:30:36: error: '(size: Size)' does not have a member named 'width' | |
println("rectangle with size \(size.width)x\(size.height)") | |
^ ~~~~~ | |
*/ | |
struct Size { | |
var width:Double | |
var height:Double | |
} | |
enum Shape { | |
case Circle(radius:Double) | |
case Rectangle(size:Size) | |
} | |
func printShape(shape:Shape) { | |
switch shape { | |
case .Circle(let radius): | |
println("circle with radius \(radius)") | |
case .Rectangle(let size): | |
// This version does work | |
let s = size | |
println("rectangle with size \(s.width)x\(s.height)") | |
// Does not work: | |
println("rectangle with size \(size.width)x\(size.height)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment