模式匹配第三弹: 自定义的模式匹配和语法糖

YvetteFMPU 8年前
   <p>在模式匹配系列文章的<a href="http://www.open-open.com/lib/view/open1461650808637.html" rel="external">第一弹</a>和<a href="http://www.open-open.com/lib/view/open1461743259402.html" rel="external">第二弹</a>中,我们已经看到关于 switch 搭配很多类型的用法,包括元组(<code>tuples</code>),范围(<code>Range</code>),字符串(<code>String</code>),符号(<code>Character</code>)和一些其他类型。但是假如我们使用自定义的类型和模式匹配,又能擦出怎样的火花呢?</p>    <h2>Switch 和模式匹配操作符</h2>    <p>如果你在 <code>switch</code> 实例中这样写 <code>case 1900..<2000</code>,那么 Swift 如何比较 switch 入口的单值与下面的范围?</p>    <p>答案非常简单:Swift 使用了 <code>~=</code> 操作符。当你在 case 中使用 <code>Range<I></code>时,switch 可以对 <code>I</code> 进行匹配,这是因为<code>Range<I></code> 与 <code>I</code> 二者之间定义了 <code>~=</code> 操作符:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">func ~=<I : ForwardIndexType where I : Comparable>(pattern: Range<I>, value: I) -> Bool  </code></pre> </td>      </tr>     </tbody>    </table>    <p>事实上,如果你写 <code>switch someI</code> 并加上 <code>case aRangeOfI</code> 语句时,Swift 会尝试调用 <code>aRangeOfI ~= someI</code> 来做匹配操作(该表达式会返回一个 Bool 来通知是否匹配成功)</p>    <p>这就意味着你可以为自己的类型定义相同的操作符 <code>~=</code>,这样就能保证这些自定义类型可以在 <code>switch/case</code> 语句中使用,我们也可以用相同的方式使用 <code>Range</code>!</p>    <h2>让你的自定义类型响应模式匹配</h2>    <p>我们构造一个自定义的结构体:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">struct Affine {    var a: Int    var b: Int  }    func ~= (lhs: Affine, rhs: Int) -> Bool {    return rhs % lhs.a == lhs.b  }    switch 5 {  case Affine(a: 2, b: 0): print("Even number")  case Affine(a: 3, b: 1): print("3x+1")  case Affine(a: 3, b: 2): print("3x+2")  default: print("Other")  }  </code></pre> </td>      </tr>     </tbody>    </table>    <p>最终打印的结果是 <code>3x+2</code>!</p>    <p>值得注意的一点是:在使用自定义类型时,Swift 不知道 switch 是否穷尽了所有可能。例如,即使我们添加了<code>case Affine(a: 2, b: 1)</code> 和 <code>case Affine(a: 2, b: -1)</code> 这两个子句,来覆盖到每一个正整数和负整数的情况,Swift 还是会强迫我们使用 <code>default:</code> 语句。</p>    <p>此外需要注意,不要搞混了参数的顺序:<code>~=</code> 的第一个参数(通常称为 <code>lhs</code>,译指左手边)是你将要在 <code>case</code> 子句中使用的对象,第二个参数(通常称为 <code>rhs</code>,译指右手边)是你使用 switch 传入的对象。</p>    <h2>~= 的一些其他用途</h2>    <p><code>~=</code> 还有很多其他用途。</p>    <p>例如,我们可以在<a href="http://www.open-open.com/lib/view/open1461743259402.html" rel="external">第二弹</a>中登场的 <code>Book</code> 结构体中添加如下代码:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">func ~= (lhs: Range<Int>, rhs: Book) -> Bool {    return lhs ~= rhs.year  }  </code></pre> </td>      </tr>     </tbody>    </table>    <p>现在测试一下:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">let aBook = Book(title: "20,000 leagues under the sea", author: "Jules Vernes", year: 1870)  switch aBook {  case 1800..<1900: print("19th century book")  case 1900..<2000: print("20th century book")  default: print("Other century")  }  </code></pre> </td>      </tr>     </tbody>    </table>    <p>当然我不鼓励这样使用,将 Book 直接与一段整数范围比较并不能很清晰地展现我们的意图:『其实是想与 book 的出版年份进行比较』。更好的方式是在 switch 中直接传入 <code>aBook.year</code>。但我们举这个例子只是为了展示 <code>~=</code> 操作符的强大(另一个原因是我暂时想不到更好的例子了)。</p>    <p>另一个使用 ~= 的例子是判定一个字符串是否『足够接近』另一个。例如,你正在创作一个测试游戏,并且期望玩家通过键盘来输入答案,对于玩家的答案,你希望大小写不敏感,变音符号不敏感,甚至容忍一些小错误,可以像下面这样写:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">struct Answer {    let text: String    let compareOptions: NSStringCompareOptions = [.CaseInsensitiveSearch, .DiacriticInsensitiveSearch, .WidthInsensitiveSearch]  }    func ~= (lhs: Answer, rhs: String) -> Bool {    return lhs.text.compare(rhs, options: lhs.compareOptions, range: nil, locale: nil) == NSComparisonResult.OrderedSame  }    let question = "What's the French word for a face-to-face meeting?"  let userAnswer = "Tete a Tete"    switch userAnswer {  case Answer(text: "tête-à-tête"): print("Good answer!")  case Answer(text: "tête à tête"): print("Almost… don't forget dashes!")  default: print("Sorry, wrong answer!")  }  // 输出 "Almost… don't forget dashes!"  </code></pre> </td>      </tr>     </tbody>    </table>    <p>观察一下,这种比较是如何在一个区分大小写,变音符合不敏感,宽度不敏感的比较中得到一个最接近的答案的。</p>    <h2>Optionals 的语法糖</h2>    <p>如果你认为 <code>switch</code> 和匹配模式的语法糖就是在 <code>switch/case</code> 语句中透明地使用 <code>~=</code>,那只能说你们呐 <strong>too young too simple,sometimes naive</strong>。</p>    <p>你需要了解的另一个有用的语法糖就是:当 switch 处理一个可选值 <code>x?</code> 时,你可以识别问号标记的可选值。</p>    <p>在这种特殊的环境下,使用 <code>x?</code> 作为语法糖来表示 <code>.Some(x)</code>,这就意味着你可以这样写:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">let anOptional: Int? = 2  switch anOptional {  case 0?: print("Zero")  case 1?: print("One")  case 2?: print("Two")  case nil: print("None")  default: print("Other")  }  </code></pre> </td>      </tr>     </tbody>    </table>    <p>事实上,如果你不使用 ? 而是用 <code>case 2:</code> 来替代 <code>case 2?:</code>,那么编译器会报错:</p>    <p><code>expression pattern of type 'Int' cannot match values of type 'Int?'</code> 因为你尝试将 <code>Int(2)</code> 和一个 <code>Int?</code>(可选值)进行匹配。</p>    <p>使用 case 2?: 其实就精准地等同于写 case Optional.Some(2),这一过程将产生一个可选<br> 值 <code>Int?</code> 包含整数 <code>2</code>,这样我们就能开心地匹配另一个可选值 <code>Int?</code> 了。就像 <code>anOptional. case 2?:</code> 其实是<code>.Some(2)</code> 的一种更紧凑的形式。</p>    <h2>游走在枚举对象 rawValue 上的 Switch</h2>    <p>提起这个,我最近构建一个 <code>UITableView</code> 作为菜单时,偶然发现一些代码可以使用枚举 <code>enum</code>(配合 <code>Int</code> 原始值 rawValue) 来组织。我们随后可以直接操作 enum MenuItem(枚举子菜单)而不是 <code>indexPath.row</code>,这个想法是不是棒极了呢!</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">enum MenuItem: Int {    case Home    case Account    case Settings  }  </code></pre> </td>      </tr>     </tbody>    </table>    <p>随后基于 <code>MenuItem</code> 来实现每一行 tableView row,代码如下:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">switch indexPath.row {  case MenuItem.Home.rawValue: …  case MenuItem.Account.rawValue: …  case MenuItem.Settings.rawValue: …  default: ()  </code></pre> </td>      </tr>     </tbody>    </table>    <p>首先,注意 <code>switch</code> 是如何处理 <code>Int (indexPath.row)</code> 并且每一行 case 如何使用 <code>rawValue</code> 的。这样做是错误的,原因有如下几个:</p>    <ul>     <li>首先这种写法不会阻止你使用其他任意值,比如一个复制粘贴能使你写出 <code>case FooBar.Baz.rawValue</code> 然后编译器也不会抱怨。但是你是在处理 <code>MenuItems</code>,所以还是希望编译器能够确保我们处理的是 <code>MenuItems</code>,而不是其他东东。</li>     <li>另一个问题是 <code>switch</code> 自身没有穷尽所有可能,这就是为什么 <code>default</code> 语句是必要的。我强烈推荐你尽量不使用<code>default</code> 语句,而是在 <code>switch</code> 中穷尽所有可能,按照这种方式如果你新添加一个值到枚举对象中,你不由自主地会强迫自己去思考如何去做而不是什么都不去想,更不会都依赖 <code>default</code> 来处理那些因为你懒而没有意识的情形。</li>    </ul>    <p>为了替代 <code>switch</code> 中的 <code>indexPath</code> 和 <code>case ….rawValue</code>,你应该一开始就依赖 rawValue 构建枚举对象 enmu,通过这种方式,你在 switch 中使用枚举对象的 cases 就足够了,即使用 <code>MenuItem</code> 枚举对象的 cases,而不是像 <code>FooBar.Baz</code> 之类的东东。</p>    <p>这样做,因为 <code>MenuItem(rawValue:)</code> 是一个允许失败的初始化程序,事实上该方法返回的也是一个可选值 <code>MenuItem?</code>,可以用到我们上面提到过的语法糖耶!</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">switch MenuItem(rawValue: indexPath.row) {  case .Home?: …  case .Account?: …  case .Settings?: …  case nil: fatalError("Invalid indexPath!")  }  </code></pre> </td>      </tr>     </tbody>    </table>    <p>老实说,对于那种情形我更习惯使用 <code>guard let</code>,而且我发现这种方式比在每个 <code>case</code> 中使用 <code>?</code> 更具可读性:</p>    <table>     <tbody>      <tr>       <td> <pre>  <code class="language-swift">guard let menuItem = MenuItem(rawValue: indexPath.row) else { fatalError("Invalid indexPath!") }  switch menuItem {  case .Home: …  case .Account: …  case .Settings: …  }  </code></pre> </td>      </tr>     </tbody>    </table>    <p>是不是有点灵活,了解一下所有可能的选择总归不是一件坏事。</p>    <h2>总结</h2>    <p>这就是今天我要说的,在这一系列文章的下一篇(或许是最后一篇)中,我会说说如何在其他场景下使用模式匹配。我们不会局限在 <code>switch</code> 上,会讲讲在 <code>if</code>, <code>guard</code> 和 <code>for</code> 循环中如何使用模式匹配,并将大家对模式匹配的认识提升到一个新的高度,让我们拭目以待吧。</p>    <p>来源:http://swift.gg/2016/04/28/pattern-matching-3/</p>