Consider the below code:
object Rough extends App {
trait Sample
object Sample {
def apply[String](body: => String): java.lang.String = {
"Hello " + body
}
}
val s: String = Sample {
"World"
}
print(s)
}
In the code, when I can understand that the apply is being called. But I am not able to understand:
- What does this syntax mean:
Sample{"World"}
? What is it called as and how does it invokeapply()
? - What does
body: => String
inapply()
mean? Why does it give me an error if there is no space betweenbody:
and=>
? - Why does it ask me to type
java.lang.String
as return instead of justString
?