“:” in type parameter

拟墨画扇 提交于 2019-12-05 05:25:55
Eastsun
def managed[A : Resource : Manifest](opener : => A) : ManagedResource[A] = new DefaultManagedResource(opener)

means

def managed[A](opener : => A)(implicit r: Resource[A], m: Manifest[A]) : ManagedResource[A] = new DefaultManagedResource(opener)

You can look link text 7.4 Context Bounds and View Bounds for more information.

Using a simpler example to illustrate:

def method[T : Manifest](param : T) : ResultType[T] = ...

The notation T : Manifest means that there is a context bound. Elsewhere in your program, in scope, must be defined a singleton or value of type Manifest[T] that's marked as an implicit.

This is achieved by the compiler rewriting the method signature to use a second (implicit) parameter block:

def method[T](param : T)(implicit x$1 : Manifest[T]) : ResultType[T] = ...

As your example illustrates, multiple context bounds can be used in the same method signature. It's also possible to combine them with view bounds.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!