为什么不能在switch语句中使用元组常量作为大小写 [英] Why can't I use a tuple constant as a case in a switch statement

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

问题描述

我决定使用Swift的case语句和元组。

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

我决定使用月/日/年元组。令我惊讶的是,我不能在switch语句中使用恒定的元组值作为案例。这是一个示例(可以粘贴到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次崩溃。Yayyy!!错误是二进制运算符〜= 无法应用于两个mdyTuple歌剧nds。

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."

为什么要尝试使用〜= 操作数? $ p $$$$$$

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?

推荐答案

您可以为<$ c实现〜= 运算符$ c> 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
}

,并针对以下内容实现:

and is implemented for the following:

/// 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语句中使用元组常量作为大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆