Skip to content

Instantly share code, notes, and snippets.

@tjw
Created September 13, 2014 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjw/e1e3bac96f2b896e841c to your computer and use it in GitHub Desktop.
Save tjw/e1e3bac96f2b896e841c to your computer and use it in GitHub Desktop.
switch with enum case with struct associated value
/*
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