jpswain
jpswain

Reputation: 14732

How to name parameters for a function in a method signature in scala?

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!

Upvotes: 1

Views: 60

Answers (1)

wedens
wedens

Reputation: 1830

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

Upvotes: 2

Related Questions