Scala 中的简明条件运算符
Concise conditional operator in Scala
让我演示一下我对条件运算符的期望:
import scalaz._
import Scalaz._
import util.Random
trait Card
class BigCard extends Card
class SmallCard extends Card
object Main extends App {
def printCard(card: Card) {println(card.getClass.getSimpleName)}
// Line bellow works fine, but I would prefer less verbose conditional operator (like ?: from JavaScript).
(if (Random.nextBoolean) new BigCard else new SmallCard) |> printCard
// I thought ScalaZ's ?| was meant to be an alternative to if. But following statement fails to compile.
(Random.nextBoolean ? new BigCard | new SmallCard) |> printCard
}
结果:
[error] xxx/Main.scala:15: type mismatch;
[error] found : SmallCard
[error] required: BigCard
[error] (Random.nextBoolean ? new BigCard | new SmallCard) |> printCard
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
是否有 ScalaZ 的替代方案?|支持子类的运算符(不确定术语,还是类型扩展)?
我正在寻找一个简洁的条件运算符(因此手动添加类型是不可能的,它最终会比 if
更长更丑陋)。是否可以轻松添加(如没有自定义宏)或某些库是否提供此类运算符?
是的,这很容易(implicit class
不能在顶层声明,将其放入 object
并导入其内容):
class Cond[A](x: Boolean, value: => A) {
def |[B >: A](other: => B) = if (x) value else other
}
implicit class CondOp(x: Boolean) {
def ?[A](y: => A) = new Cond(x, y)
}
Scalaz 显然定义了 def |(other: A)
。
缺点(大概也是 Scalaz 不这样做的原因)是它还针对不相关的类型进行编译,因为在 Scala 中 any 两种类型具有共同的超类型:Random.nextBoolean ? 1 | ""
。此外,对于 x ? 1 | 1.0
,您不会像 if
那样扩大到 Double
;它 returns AnyVal
而不是。
让我演示一下我对条件运算符的期望:
import scalaz._
import Scalaz._
import util.Random
trait Card
class BigCard extends Card
class SmallCard extends Card
object Main extends App {
def printCard(card: Card) {println(card.getClass.getSimpleName)}
// Line bellow works fine, but I would prefer less verbose conditional operator (like ?: from JavaScript).
(if (Random.nextBoolean) new BigCard else new SmallCard) |> printCard
// I thought ScalaZ's ?| was meant to be an alternative to if. But following statement fails to compile.
(Random.nextBoolean ? new BigCard | new SmallCard) |> printCard
}
结果:
[error] xxx/Main.scala:15: type mismatch;
[error] found : SmallCard
[error] required: BigCard
[error] (Random.nextBoolean ? new BigCard | new SmallCard) |> printCard
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
是否有 ScalaZ 的替代方案?|支持子类的运算符(不确定术语,还是类型扩展)?
我正在寻找一个简洁的条件运算符(因此手动添加类型是不可能的,它最终会比 if
更长更丑陋)。是否可以轻松添加(如没有自定义宏)或某些库是否提供此类运算符?
是的,这很容易(implicit class
不能在顶层声明,将其放入 object
并导入其内容):
class Cond[A](x: Boolean, value: => A) {
def |[B >: A](other: => B) = if (x) value else other
}
implicit class CondOp(x: Boolean) {
def ?[A](y: => A) = new Cond(x, y)
}
Scalaz 显然定义了 def |(other: A)
。
缺点(大概也是 Scalaz 不这样做的原因)是它还针对不相关的类型进行编译,因为在 Scala 中 any 两种类型具有共同的超类型:Random.nextBoolean ? 1 | ""
。此外,对于 x ? 1 | 1.0
,您不会像 if
那样扩大到 Double
;它 returns AnyVal
而不是。