让我演示一下我对条件运算符的期望:
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
更长且更丑)。是否可以轻松添加(如没有自定义宏)或某些库是否提供此类运算符?
答案 0 :(得分:1)
是的,这很容易(em
无法在顶级声明,将其放入implicit class
并导入其内容):
object
Scalaz显然定义了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没有这样做的原因)是它也编译了不相关的类型,因为在Scala 中任何两种类型都有一个共同的超类型:def |(other: A)
。此外,对于Random.nextBoolean ? 1 | ""
,您不会像x ? 1 | 1.0
那样扩展到Double
;它会返回if
。