Monday, January 28, 2013

Understanding the implicit on classes

When I was learning Scala I stumbled into the implicit keyword. It took me a bit to understand it, but I really got into it once I started using Play. The best way that I can explain it is by thinking about "extending" the class without actually changing the code. Here are some examples: This type of code is very useful. If you are using the Play framework, then you probably seem this type of code: In here, we are implicitly extending the HTTP request so we can inject a product list. This type of code is very useful because you can still have the Person class without been compromised (immutable/intact). The same goes for the example below: Using Anorm, we use implicit to use the SQL connection. However, as you can imagine, this can also have some problems. You can read more about it here.


  1. not sure but I think than implicit in parameters works diferent to implicit methods or implicit class..while implicit methods help extending class implicit parameters is about "search" in the context of the scope where is declared the method and pass it to the method...

    implicit var willBePassToTheFunction = 10

    def add (a : Int)(implicit b : Int) = a + b

    now you can call add with add(2,3)
    or you can call this with

    add(8) return 18 because this will try found the implicit parameter in the this case 10...

    although I can understand the concept..for me the use inside play framework still being a black magic...I can understand why is use and how is use it...:S

  2. Hi Marcelo! Great post over there, I am very new to scala and Play framework, coming from PHP, i feel lost... my main question is what means and how I must use the rocket symbol (=>). I don't understand this, sorry.