对隐式对象或值中的参数类型进行抽象?
考虑隐式对象的这种应用
trait Splitter[A,B,C] {
def split(list: List[C]):(List[A],List[B])
}
implicit object PairSplitter extends Splitter[Int, String, Pair[Int,String]] {
override def split(list: List[Pair[Int,String]]):(List[Int],List[String]) =
(list.collect{case (a,_) => a}, list.collect{case (_,b) => b})
}
implicit object EitherSplitter extends Splitter[Int, String, Either[Int,String]] {
override def split(list: List[Either[Int,String]]):(List[Int],List[String]) =
(list.collect{case Left(a) => a}, list.collect{case Right(b) => b})
}
def splitList[A,B,C](list:List[C])(implicit splitter:Splitter[A,B,C]):(List[A],List[B]) = splitter.split(list)
println(splitList(List((1,"one"),(2,"two"))).isInstanceOf[(List[Int],List[String])])
println(splitList(List[Either[Int,String]](Left(42),Right("two"))).isInstanceOf[(List[Int],List[String])])
//println(splitList(List(1,2,3,4))) //won't compile
它可以工作,但显然不是很有用。对于示例中的 Int 和 String 等具体类型编写此代码没有问题,但我认为无法编写抽象 A 和 B 的隐式对象或 val。
这可以完成吗?如何完成?如果不是,那么期望语言扩展具有这种能力是否合理?
Consider this application of implicit objects
trait Splitter[A,B,C] {
def split(list: List[C]):(List[A],List[B])
}
implicit object PairSplitter extends Splitter[Int, String, Pair[Int,String]] {
override def split(list: List[Pair[Int,String]]):(List[Int],List[String]) =
(list.collect{case (a,_) => a}, list.collect{case (_,b) => b})
}
implicit object EitherSplitter extends Splitter[Int, String, Either[Int,String]] {
override def split(list: List[Either[Int,String]]):(List[Int],List[String]) =
(list.collect{case Left(a) => a}, list.collect{case Right(b) => b})
}
def splitList[A,B,C](list:List[C])(implicit splitter:Splitter[A,B,C]):(List[A],List[B]) = splitter.split(list)
println(splitList(List((1,"one"),(2,"two"))).isInstanceOf[(List[Int],List[String])])
println(splitList(List[Either[Int,String]](Left(42),Right("two"))).isInstanceOf[(List[Int],List[String])])
//println(splitList(List(1,2,3,4))) //won't compile
It works, but obviously isn't terrible useful. It's no problem to write this for concrete types like Int and String in the example, but I see no way to write an implicit object or val which abstracts over A and B.
Can this be done, and how? If not, is it reasonable to expect a language extension which has this ability?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Scala 的值(即 val 和对象)是单态的,因此如果您坚持将隐式作为值,则没有直接的方法可以得到您想要的东西。
但是,如果它们不必是值,则有一个简单的替代方案:您可以使用可以多态的东西,隐式方法,
REPL会话......
Scala's values (ie. vals and objects) are monomorphic, so there's no direct way of getting what you're after if you insist on the implicits being values.
But there's a straightforward alternative if they don't have to be values: you can use something which can be polymorphic, an implicit method,
REPL session ...