You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In which task and step of the codelab can this issue be found?
Step 4: use functions as a data type
Describe the problem
One thing that is not clear in the training is why "5 quarters" is always printed even when I comment out the call to the treatFunction() and re-run the code.
If I never make the call to the treatFunction, how does the code get to the else component and execute the println?
Steps to reproduce?
here is my code: see how treatFunction() line is commented out with // Also, I don't see "Have a treat" printed, so I know that is working. But "5 quarters" still prints on the first line. Why?
fun main() {
val treatFunction = trickOrTreat(false) {"$it quarters"}
val trickFunction = trickOrTreat(true, null)
trickFunction()
//treatFunction()
}
fun trickOrTreat(isTrick: Boolean, extraTreat: ((Int) -> String)?): () -> Unit {
if (isTrick) {
if (extraTreat != null) {
println(extraTreat(2))
}
return trick
} else {
if (extraTreat != null) {
println(extraTreat(5))
}
return treat
}
}
val trick = {
println("No treats!")
}
val treat = {
println("Have a treat!")
}
Versions
I'm using Kotlin playground to test the code as instructed.
Additional information
screenshot included:
The text was updated successfully, but these errors were encountered:
URL of codelab
https://developer.android.com/codelabs/basic-android-kotlin-compose-function-types-and-lambda?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-2-pathway-1%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-function-types-and-lambda#5
In which task and step of the codelab can this issue be found?
Step 4: use functions as a data type
Describe the problem
One thing that is not clear in the training is why "5 quarters" is always printed even when I comment out the call to the treatFunction() and re-run the code.
If I never make the call to the treatFunction, how does the code get to the else component and execute the println?
Steps to reproduce?
here is my code: see how treatFunction() line is commented out with // Also, I don't see "Have a treat" printed, so I know that is working. But "5 quarters" still prints on the first line. Why?
fun main() {
val treatFunction = trickOrTreat(false) {"$it quarters"}
val trickFunction = trickOrTreat(true, null)
trickFunction()
//treatFunction()
}
fun trickOrTreat(isTrick: Boolean, extraTreat: ((Int) -> String)?): () -> Unit {
if (isTrick) {
if (extraTreat != null) {
println(extraTreat(2))
}
return trick
} else {
if (extraTreat != null) {
println(extraTreat(5))
}
return treat
}
}
val trick = {
println("No treats!")
}
val treat = {
println("Have a treat!")
}
Versions
I'm using Kotlin playground to test the code as instructed.
Additional information
screenshot included:
The text was updated successfully, but these errors were encountered: