Looking at the method signature of 'intercept' within scala test :
def intercept[T <: AnyRef](f: => Any)(implicit manifest: Manifest[T]): T = {
I dont know how [T <: AnyRef] is used ? This looks like a parameter type but why is it contained within angled brackets - [] ?
Here is the complete method :
def intercept[T <: AnyRef](f: => Any)(implicit manifest: Manifest[T]): T = {
val clazz = manifest.erasure.asInstanceOf[Class[T]]
val caught = try {
f
None
}
catch {
case u: Throwable => {
if (!clazz.isAssignableFrom(u.getClass)) {
val s = Resources("wrongException", clazz.getName, u.getClass.getName)
throw newAssertionFailedException(Some(s), Some(u), 4)
}
else {
Some(u)
}
}
}
caught match {
case None =>
val message = Resources("exceptionExpected", clazz.getName)
throw newAssertionFailedException(Some(message), None, 4)
case Some(e) => e.asInstanceOf[T] // I know this cast will succeed, becuase iSAssignableFrom succeeded above
}
}
This language construct is called type parameterization, and you can read more about it here:
http://www.artima.com/pins1ed/type-parameterization.html
In this case a method can also declare type parameter. If you already have some Java background, then here is very similar Java equivalent:
public <T extends Object> T intercept(Runnable f) {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With