apply
is amagicScala method. There is no need to callapply
explicitly to execute it. And this is part of the tool that thecase class
use.
This SKB might be a bit longer than previous ones.
apply
can be called like any other method (Person.apply(...)
) but it can be used without calling its name explicitely (Person(...)
). And that's it.
If you want to learn more, continue reading.
In fact, you could implement a function yourself:
val add = new Function2[Int, Int, Int] {
def apply(a: Int, b: Int): Int = a + b
}
val result = add(1, 2)
assert(result == 3)
Try and copy it above in the editor.
You could also implementif
yourself ! Try it by yourself before looking at Scastie.
Solution:Scastie (owFP4D18R6yjrDJoPLu7Bg)