很早前在来往的扎堆里发过一段模拟C#的using代码:
def using[ C <: { def close(): Unit }, T ] (resource: C) (handle: C => T): T = {
try {
handle(resource)
} finally {
closeQuietly(resource)
}
}
Eric(药师)看到后,说 <%
更好
<%
的意思是“view bounds”(视界),它比<:
适用的范围更广,除了所有的子类型,还允许隐式转换过去的类型.它不出错的前提条件是A是B的子类型或者A能够隐式转换到B,否则method方法是存在问题的。
def method [A <% B](arglist): R = ...
def method [A](arglist)(implicit viewAB: A => B): R = ...
或等价于:
implicit def conver(a:A): B = …
在后面两种方式里,已经写明了存在A到B的隐式转换,因此这两种method自然不会出现问题。
def method [A](arglist): R = ...
<%
除了方法使用之外,class声明类型参数时也可使用:
scala> class A[T <% Int]
defined class A
但无法对trait的类型参数使用 <%
,
scala> trait A[T <% Int]
<console>:1: error: traits cannot have type parameters with context bounds `: ...' nor view bounds `<% ...'
补充,注意上面 using 方法中声明的结构类型 {def close():Unit}
,它的实现是通过反射做到的,所以有一定性能问题,在需要考虑性能的场景下,这里明确的用java.io.Closeable
接口更合适,灵活性与性能你总要做一个选择。