當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Scala Either.cond用法及代碼示例

cond 方法(或屬性)屬於 scala.util.Either 對象(object),其相關用法說明如下。

用法:

def cond[A, B](test: Boolean, right: => B, left: => A): Either[A, B]

如果條件滿足,則在Right中返回給定的B,否則,在Left中返回給定的A.

val userInput: String = readLine()
Either.cond(
  userInput.forall(_.isDigit) && userInput.size == 10,
  PhoneNumber(userInput),
  s"The input ($userInput) does not look like a phone number"

源碼:

Either.scala

相關用法


注:本文由純淨天空篩選整理自scala-lang.org大神的英文原創作品 Either.cond。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。