如何定义“类型分离”(联合类型)?有一种方法建议处理重载方法的双重定义是将重载替换为模式匹配: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..有谁知道实现同样目的替代解决方案吗?据我所知,Scala没有内置的“类型分离”。另外,上面定义的隐式转换是否潜伏在某个标准库中,以便我只需导入它们?
3 回答
慕容708150
TA贡献1831条经验 获得超4个赞
Any*
class StringOrInt[T]object StringOrInt { implicit object IntWitness extends StringOrInt[Int] implicit object StringWitness extends StringOrInt[String]}
foo
object Bar { def foo[T: StringOrInt](x: T) = x match { case _: String => println("str") case _: Int => println("int") }}
foo(5)
foo("abc")
foo(true)
StringOrInt[Boolean]
StringOrInt
a sealed
T: StringOrInt
StringOrInt[T]
红糖糍粑
TA贡献1815条经验 获得超6个赞
scala> def f[A](a: A)(implicit ev: (Int with String) <:< A) = a match { | case i: Int => i + 1 | case s: String => s.length | }f: [A](a: A)(implicit ev: <:<[Int with String,A])Intscala> f(3)res0: Int = 4scala> f("hello")res1: Int = 5scala> f(9.2)<console>:9: error: Cannot prove that Int with String <:< Double. f(9.2) ^
资料来源:
添加回答
举报
0/150
提交
取消