Using Scala structural types with abstract types

对着背影说爱祢 提交于 2019-12-03 12:17:42

As you can see in ticket 1906 you can't use the abstract type defined outside the structural type due to missing type information at runtime.

This is stated in the Scala Language Reference (3.2.7 Compound Types):

Within a method declaration in a structural refinement, the type of
any value parameter may only refer to type parameters or abstract types that are
contained inside the refinement.

The usual way to add new methods to a type is by implicit type conversion.

trait HigherOrderFunctions[T, CC[_]] {
    def zap[V](fn: () => V): CC[V]
}

class RichJList[T](list: java.util.List[T]) extends HigherOrderFunctions[T, java.util.List]{
    def zap[V](fn: () => V): java.util.List[V] = {
        val l = new java.util.ArrayList[V]
        l add fn()
        l
    }
}
implicit def list2RichList[T](l : java.util.List[T]) = new RichJList(l)
new java.util.ArrayList[AnyRef]() zap (() => 2)

If the compiler sees that the type missed the zap method it will convert it to a type that has the zap method with a implicit conversion method (here list2RichList) in scope.

scala> new java.util.ArrayList[AnyRef]() zap (() => 2)
res0: java.util.List[Int] = [2]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!