Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
fun String.shout() = "${uppercase()}!"
fun main() {
val s = "hi"
println(s.shout())
println(s)
}HI! hi
- Expecting
HI! HI!: thinkingshoutchanges the string it is called on. It only returns a new string, andsstill holdshi. - Expecting a compile error: thinking you can add functions only to your own classes. An extension adds
shouttoStringwithout opening it.
- How would you make
sitself holdHI!after the call? - What does
thisrefer to insideshout?