":" in type parameter
Asked Answered
O

2

8

In scala-arm project, I see code like this:

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

Can someone explain the meaning of [A : Resource : Manifest] ?

Outlander answered 26/9, 2010 at 14:51 Comment(1)
#2982776Mesencephalon
H
18
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.

Hurryscurry answered 26/9, 2010 at 15:18 Comment(0)
T
4

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.

Trichoid answered 27/9, 2010 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.