Why can't I use the tuple constant as a case in the switch statement

I decided to play with Swift and tuple statements. It looks like one of the more complex language features.

I decided to play with the tuples month / day / year. To my surprise, I cannot use the constant value of a tuple as a case in a switch statement. Here is an example (you can insert into the playing field and run)

import UIKit
typealias mdyTuple = (month: Int, day: Int, year: Int)
let joesBirthday: mdyTuple = (month: 6, day: 7, year: 1978)
let someday: mdyTuple = (6, 7, 1978)

switch someday
{
  //---------
  //The line "case joesBirthday" won't compile.
  //case joesBirthday:
  //  println("Joe was born on this day"
  //---------
case (joesBirthday.month, joesBirthday.day, joesBirthday.year):
  println("Joe was born on this day")
case (joesBirthday.month, joesBirthday.day, let year):
  println("Joe is \(year-joesBirthday.year) today")
default:
  println("Some other day")
}

Commented code case joesBirthday:will not compile (in Xcode 6.3, if that matters). The following case (where I list all the elements of the joesBirthday tuple separately), which is harder to enter and harder to read, works)

My playground crashed Xcode when I typed this message and AGAIN worked, trying to restart Xcode, so I can not report the error code.

, , , Xcode, ( 4 . Yayyy!) : " ~= mdyTuple."

~=? ?

, switch?

+4
1

~= mydTuple :

func ~=(a: mdyTuple, b: mdyTuple) -> Bool {
    return a.month ~= b.month && a.year ~= b.year && a.day ~= b.day
}

...

switch someday {
case joesBirthday:
    println("one")
default:
    println("two")
}

"".

:

infix operator ~= {
    associativity none
    precedence 130
}

:

/// Returns `true` iff `pattern` contains `value`
func ~=<I : IntervalType>(pattern: I, value: I.Bound) -> Bool
func ~=<T>(lhs: _OptionalNilComparisonType, rhs: T?) -> Bool
func ~=<T : Equatable>(a: T, b: T) -> Bool
func ~=<I : ForwardIndexType where I : Comparable>(pattern: Range<I>, value: I) -> Bool
+4

All Articles