google-developer-training / basic-android-kotlin-compose-training-dice-roller

Apache License 2.0
58 stars 50 forks source link

Use function types and lambda expressions in Kotlin: Android Basics with Compose #99

Open ur-liza opened 1 year ago

ur-liza commented 1 year ago

URL of codelab https://developer.android.com/codelabs/basic-android-kotlin-compose-function-types-and-lambda?hl=en#5

In which task and step of the codelab can this issue be found? "Run your code. The "Have a treat" string should print four times."

Describe the problem The problem is that there is no explanation why 5 quarters is printed out only once, instead of four times.

Steps to reproduce?

  1. Go to...
  2. Click on...
  3. See error...

Versions Android Studio version: API version of the emulator:

Additional information Include screenshots if they would be useful in clarifying the problem.

NemesisJWD commented 12 months ago

Man that drives me nuts, dear @AndroidDevelopers please add a green hint box to the tutorial explaining the circumstance. It gets even crazier when you exchange the treatFunction within the repeat with the trickOrTreat. In that case it prints "5 quarters" four times but not "Have a treat!".

Elaws commented 9 months ago

So why both 5 quarters and Have a treat aren't repeated 5 times ? Did you find the explanation @NemesisJWD and @ur-liza ?

Thank you.

Elaws commented 9 months ago

Ok, got the answer on the AndroidDev discord.

5 quarters is printed only once because it is executed only once (when treatFunction is affected by trickOrTreat(false) { "$it quarters" }) ! Then, note that the trickOrTreat function only returns trick or treat. In our case, treatFunction is equal to treat function only ! So when we repeat treatFunction 4 times, it only calls treat function 4 times (and not at all the whole trickOrTreat(false) { "$it quarters" }) !!