为什么这种结构类型绑定不能按预期工作?
我正在尝试编写一个简单的辅助方法,它接收可以关闭的东西和一些接收前者的函数,并确保在执行函数后关闭“closeable”。
例如,我想像这样使用它:
closing(new FileOutputStream("/asda"))(_.write("asas"))
我的意思是
object Helpers {
def closing[T <: { def close }](closeable: T)(action: T => Unit): Unit =
try action apply closeable finally closeable close
}
但是在尝试编译这个简单的测试时:
object Test {
import Helpers._
closing(new FileOutputStream("/asda"))(_.write("asas"))
}
编译器抱怨:
推断的类型参数
[java.io.FileOutputStream]没有
符合方法结束的类型
参数界限[T&lt ;: AnyRef {def
关闭:单位}]
有什么想法吗?
没有找到相关结果
已邀请:
2 个回复
膛嵌墒缅欠
Scala与空括号的方法和没有括号的方法之间存在差异。
豪抱怒掳
在你的情况下,你想要
附:如果你真的打算大量使用它,你可能也应该玩
并查看每种情况下需要使用哪种ѭ7。