def downloadResultsPage(onPageDownload: (List[String], Boolean) => Unit) {
body here...
}
在此示例中,onPageDownload是在页面完成时将调用的闭包。为了使代码自我更好,我真的希望能够有这样的东西:
(onPageDownload: (results: List[String], finished: Boolean) => Unit)
有办法做到这一点吗?
谢谢!
答案 0 :(得分:2)
您可以创建类型别名,例如type DownloadCompletedHook = (List[String], Boolean) => Unit
,或者您可以创建特征,然后如果希望您的函数具有此特征,则隐式转换:
trait DownloadCompletedHook extends ((List[String], Boolean) => Unit) {
def apply(results: List[String], finished: Boolean): Unit
}
implicit def funToTrait(f: (List[String], Boolean) => Unit): DownloadCompletedHook = new DownloadCompletedHook {
def apply(results: List[String], finished: Boolean): Unit = f(results, finished)
}
更简单的方法(但我认为语义较少)是将results: List[String], finished: Boolean
分组到案例类