为什么我不能在 switch 语句中使用元组常量作为案例

Why can#39;t I use a tuple constant as a case in a switch statement(为什么我不能在 switch 语句中使用元组常量作为案例)

本文介绍了为什么我不能在 switch 语句中使用元组常量作为案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定使用 Swift 案例语句和元组.它看起来像是该语言最酷的功能之一.

I decided to play with Swift case statements and tuples. It looks like one of the cooler features of the language.

我决定使用月/日/年元组.令我惊讶的是,我不能在 switch 语句中使用常量元组值作为 case.这是一个示例(可以粘贴到 Playground 中并运行)

I decided to play with month/day/year tuples. To my surprise, I can't use a constant tuple value as a case in a switch statement. Here is an example (can be pasted into a Playground 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")
}

注释掉的代码 case joesBirthday: 将无法编译(在 Xcode 6.3 中,如果重要的话).下面的例子(我分别列出了 joesBirthday 元组的所有元素)既难以输入,又难以阅读,确实有效)

The commented out code, case joesBirthday:, will not compile (in Xcode 6.3, if that matters). The case below (where I list all the elements of the joesBirthday tuple separately) which is both harder to type, and harder to read, does work)

我的 Playground 在键入此内容时使 Xcode 崩溃,并在尝试重新启动 Xcode 时再次崩溃,因此我无法报告错误代码.

My Playground crashed Xcode when typing this up, and crashed AGAIN trying to restart Xcode, so I'm having trouble reporting the error code.

好的,我终于让 Xcode 停止崩溃(在连续 4 次崩溃之后.耶耶!)错误是二进制运算符 ~= 不能应用于两个 mdyTuple 操作数."

Ok, I finally got Xcode to stop crashing (after 4 crashes in a row. Yayyy!) The error is "Binary operator ~= cannot be applied to two mdyTuple operands."

为什么要尝试使用 ~= 操作数?元组不是相等的吗?

Why is it trying to use the ~= operand? Aren't like tuples equatable?

是否有一些 clean 替代语法可以让我在 switch 语句的情况下使用常量元组?

Is there some clean alternative syntax that lets me use a constant tuple in a case of a switch statement?

推荐答案

您可以像这样为 mydTuple 类型实现 ~= 运算符:

You could implement the ~= operator for the mydTuple type like this:

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

这在操场上对我有用...现在,这段代码

That worked for me in a Playground... Now, this code

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

这篇关于为什么我不能在 switch 语句中使用元组常量作为案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么我不能在 switch 语句中使用元组常量作为案例

基础教程推荐