Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Last active November 4, 2020 02:41
Show Gist options
  • Save rupeshtr78/a260fd02ae4bdc1356d6d7a71a0c4c94 to your computer and use it in GitHub Desktop.
Save rupeshtr78/a260fd02ae4bdc1356d6d7a71a0c4c94 to your computer and use it in GitHub Desktop.
apply method
object Greet {
def apply(name: String): String = {
"Hello %s".format(name)
}
}
// I can call apply explicitly if I want:
Greet.apply("bob") // => "Hello bob"
// Or I can call Greet like it is a function:
Greet("bob") // => "Hello bob"
///////////////
class Amazing {
def apply(x: String) = "Amazing %s!".format(x)
}
val amazing = new Amazing()
amazing("world") // => Amazing world!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment