日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Scala教程之:Either

發(fā)布時間:2024/2/28 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Scala教程之:Either 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在之前的文章中我們提到了Option,scala中Option表示存在0或者1個元素,如果在處理異常的時候Option就會有很大的限制,因為Option如果返回None,那么我并不知道具體的異常到底是什么,這樣scala引入了Either。

顧名思意,Either表示或者是這一個元素或者是那個元素。這樣在異常處理的時候就非常有用了。

我們先看一下Either的定義:

sealed abstract class Either[+A, +B] extends Product with Serializable

它有兩個子類Left和Right:

/** The left side of the disjoint union, as opposed to the [[scala.util.Right]] side.** @author <a href="mailto:research@workingmouse.com">Tony Morris</a>, Workingmouse*/ final case class Left[+A, +B](@deprecatedName('a, "2.12.0") value: A) extends Either[A, B] {def isLeft = truedef isRight = false@deprecated("Use .value instead.", "2.12.0") def a: A = value }/** The right side of the disjoint union, as opposed to the [[scala.util.Left]] side.** @author <a href="mailto:research@workingmouse.com">Tony Morris</a>, Workingmouse*/ final case class Right[+A, +B](@deprecatedName('b, "2.12.0") value: B) extends Either[A, B] {def isLeft = falsedef isRight = true@deprecated("Use .value instead.", "2.12.0") def b: B = value }

我們通過這兩個子類從兩個可能的元素中選擇一種。

Either 概念的產(chǎn)生時間早于Scala。很長時間以來它被認為是拋出異常的一種替代方案。
為了尊重歷史習(xí)慣,當Either 用于表示錯誤標志或某一對象值時,Left 值用于表示錯誤標志,如:信息字符串或下層庫拋出的異常;而正常返回時則使用Right 對象。很明顯,Either 可以用于任何需要持有某一個或另一個對象的場景中,而這兩個對象的類型可能不同。

我們看下怎么用Either的常規(guī)使用:

def positive(i: Int): Either[String,Int] = if (i > 0) Right(i) else Left(s"nonpositive number $i")for {i1 <- positive(5).righti2 <- positive(10 * i1).righti3 <- positive(25 * i2).righti4 <- positive(2 * i3).right } yield (i1 + i2 + i3 + i4) // Returns: scala.util.Either[String,Int] = Right(3805)for {i1 <- positive(5).righti2 <- positive(-1 * i1).right // EPIC FAIL!i3 <- positive(25 * i2).righti4 <- positive(-2 * i3).right // EPIC FAIL! } yield i1 + i2 + i3 + i4 // Returns: scala.util.Either[String,Int] = Left(nonpositive number -5)

再看一下Either怎么在代碼中消除程序錯誤,將錯誤封裝在Either中。

scala> def addInts(s1: String, s2: String): Int = | s1.toInt + s2.toInt addInts: (s1: String, s2: String)Int scala> for { | i <- 1 to 3 | j <- 1 to i | } println(s"$i+$j = ${addInts(i.toString,j.toString)}") 1+1 = 2 2+1 = 3 2+2 = 4 3+1 = 4 3+2 = 5 204 | 第7 章 3+3 = 6 scala> addInts("0", "x") java.lang.NumberFormatException: For input string: "x"

先看上面的例子,我們定義了一個addInts方法,接收兩個String參數(shù),并將其轉(zhuǎn)換為Int。如果兩個參數(shù)都是可以轉(zhuǎn)換的字符串當然沒問題,但是如果輸入了一個無法轉(zhuǎn)換的字符串就會報異常。

雖然異常有時候是好事情,但是異常會阻止程序的正常運行。我們看下怎么用Either來將其封裝起來:

scala> def addInts2(s1: String, s2: String): Either[NumberFormatException,Int]= | try { | Right(s1.toInt + s2.toInt) | } catch { | case nfe: NumberFormatException => Left(nfe) | } addInts2: (s1: String, s2: String)Either[NumberFormatException,Int] scala> println(addInts2("1", "2")) Right(3) scala> println(addInts2("1", "x")) Left(java.lang.NumberFormatException: For input string: "x") scala> println(addInts2("x", "2")) Left(java.lang.NumberFormatException: For input string: "x")

按照上面的設(shè)計,Either封裝好了異常,不會影響程序的正常運行,而且可以返回具體的錯誤信息,實在是一個不錯的設(shè)計方式。

更多精彩內(nèi)容且看:

  • 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學(xué),超級賬本,以太坊,Libra,比特幣等持續(xù)更新
  • Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續(xù)更新
  • Spring 5.X系列教程:滿足你對Spring5的一切想象-持續(xù)更新
  • java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細文章教程

更多教程請參考 flydean的博客

總結(jié)

以上是生活随笔為你收集整理的Scala教程之:Either的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。