Hello recently I come across a strange thing to me.
I declare a variable name with val and try to print it to the console the code look pretty much like this:
Copy code
fun main() {
val cup = 25
println(cup + " cup of water")
}
However this snippet of code face a compile error and the way to work around this is as follow:
Copy code
fun main() {
val cup = 25
println("" + cup + " cup of water")
}
I dont't know that in Java/Kotlin world this is normal or not but I think it's a little weird and can be improved to make a developer experience better (I come from JS field)
j
Joffrey
01/17/2023, 11:17 AM
Probably you get an error because it's using
Int.plus
and passing a string to this function is not allowed. I would suggest using string templates instead:
Copy code
val cup = 25
println("$cup cups of water")
c
CLOVIS
01/17/2023, 11:17 AM
"can be improved to make a developer experience better" → yep, that's what string templates are for 🙂
d
Dheerapat
01/17/2023, 11:25 AM
Thank you for the standard practice. 😄
r
Rasheed
01/18/2023, 6:24 AM
You should have used string Template " $cup
j
Joffrey
01/18/2023, 8:53 AM
You added a space in front that wasn't there. Why is this better than the one I suggested?