Scala apply

applyis amagicScala method. There is no need to callapplyexplicitly to execute it. And this is part of the tool that thecase classuse.

This SKB might be a bit longer than previous ones.

applycan 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 implementifyourself ! Try it by yourself before looking at Scastie.

Solution:Scastie (owFP4D18R6yjrDJoPLu7Bg)

Reveal more information and clues
Load Exercise