如何定义“类型分离”(联合类型)?
object Bar { def foo(xs: Any*) = xs foreach { case _:String => println("str") case _:Int => println("int") case _ => throw new UglyRuntimeException() }}
foo
object Bar { def foo(xs: (String or Int)*) = xs foreach { case _: String => println("str") case _: Int => println("int") }}
Either
type or[L,R] = Either[L,R]implicit def l2Or[L,R](l: L): L or R = Left(l)implicit def r2Or[L,R](r: R): L or R = Right(r)object Bar { def foo(xs: (String or Int)*) = xs foreach { case Left(l) => println("str") case Right(r) => println("int") }}
Either3
, Either4
慕容708150
红糖糍粑