Seite 1 von 1

REScala conversion functions

Verfasst: 13. Jul 2018 12:44
von haymanot
can anyone explain how the output is like 10,20,30,40.......?
val SPEED = 10
val time = Var(0)
val space = Signal{ SPEED * time() }
while (true) {
Thread sleep 20
time() = time.now + 1
}
space.changed += ((x: Int) => println(x))

-- output --
10
20
30
40

thanks

Re: REScala conversion functions

Verfasst: 13. Jul 2018 12:59
von pmueller
Hi,

the output comes from the .changed event created on the signal space.
This in turn depends on the constant SPEED and the Signal time.
You increment the value of time by one every 20 ms.
Since space depends on time, it is updated and the corresponding change event triggers and the current value of time multiplied by SPEED is printed.
Does this answer your question?

Best,
Patrick

Re: REScala conversion functions

Verfasst: 13. Jul 2018 13:11
von Eternum
I hope I'm not missing something super simple but... How does the handler ever get added to space after the infinite while(true) loop?

Re: REScala conversion functions

Verfasst: 13. Jul 2018 13:24
von pmueller
Hi,

It won't. I just assumed this was a copy/paste error.

Best,
Patrick

Re: REScala conversion functions

Verfasst: 13. Jul 2018 13:38
von haymanot
yes, thanks.