Last active
November 4, 2020 02:41
-
-
Save rupeshtr78/a260fd02ae4bdc1356d6d7a71a0c4c94 to your computer and use it in GitHub Desktop.
apply method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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