Implicit parameters
We use implicit parameters when we want the compiler to help us find a value that's already available for a certain type. We've just seen an example of an implicit parameter when we talked about Future
. Why don't we define something similar for ourselves?
We can think of a scenario where we need to show the present date in our application and we want to avoid passing a date's instance explicitly again and again. Instead, we can make the LocalDateTime.now
value implicit to the respective functions and let the current date and time be passed as an implicit parameter to them. Let's write some code for this:
import java.time.{LocalDateTime} object ImplicitParameter extends App { implicit val dateNow = LocalDateTime.now() def showDateTime(implicit date: LocalDateTime) = println(date) //Calling functions! showDateTime }
The following is the result:
2017-11-17T10:06:12.321
Think of the showDateTime
 function as the one that needs the date-time's current value...