Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is the value of the val in the closure seemingly being mutated? #23

Open
andysy opened this issue May 27, 2020 · 0 comments
Open

Why is the value of the val in the closure seemingly being mutated? #23

andysy opened this issue May 27, 2020 · 0 comments

Comments

@andysy
Copy link

andysy commented May 27, 2020

For the code below, why does xnow change along with x? e.g. I was expecting to see the "Observer N received click" to retain the N value that was assigned to xnow when the closure was initially created...

Observer 2 subscribing!
Observer 1 received click
Observer 2 received click
Observer 3 subscribing!
Observer 1 received click
Observer 3 received click
Observer 3 received click
Observer 4 subscribing!
Observer 1 received click
Observer 4 received click
Observer 4 received click
Observer 4 received click
import tornadofx.*
import io.reactivex.rxkotlin.*
import io.reactivex.Observable
import javafx.scene.control.*

class MyApp: App(MyView::class)

class MyView: View() {
  override val root = vbox {
    var clicks = button("Pressme").actionEvents()
    var x = 1
    clicks.subscribe { println("Observer 1 received click") }

    button("Subscribe New Observer") {
      setOnAction {
        x++
        println("Observer $x subscribing!")
        clicks.subscribe { val xnow=x; println("Observer $xnow received click")}
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant