1
def downloadResultsPage(onPageDownload: (List[String], Boolean) => Unit) {
    body here...
}

In this example onPageDownload is the closure that will be called when the page is completed. To make the code self-document better, I'd really like to be able to have something like this:

(onPageDownload: (results: List[String], finished: Boolean) => Unit)

Is there a way to do this?

Thanks!

1
  • write CaseClass => Unit where CaseClass is case class CaseClass(results: List[String], finished: Boolean) Commented Aug 11, 2014 at 12:43

1 Answer 1

2

you can create type alias like type DownloadCompletedHook = (List[String], Boolean) => Unit or you can create trait and then implicitly convert if you wish your functions to this trait:

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)
}

and more simple way (but less semantic i think) is to group results: List[String], finished: Boolean into case class

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.