-2

I have following application that I could not figure it out, why running it twice it breaks RT:

val program = for {
  _ <- IO { println("Welcome to Scala!  What's your name?") }
  _ <- IO { println(s"Well hello, foo") }
} yield ()

program.unsafeRunSync()
program.unsafeRunSync()

I run it twice and got the same result twice, why it breaks RT?

softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

6

It's because val a = program.unsafeRunSync(); val b = program.unsafeRunSync() is not the same program as val a = program.unsafeRunSync(); val b = a. If an expression is RT then you can inline it or factor it out freely. You can't do that here.

tpolecat
  • 438
  • 2
  • 11
  • Could you please make an example about `inline it or factor it out`? And why it is not the same? – softshipper Nov 11 '17 at 08:45
  • 1
    The first is an inlining of the second; the second is a factoring of the first. They're not the same because the first prints 4 lines of text and the second prints 2 lines. – tpolecat Nov 11 '17 at 20:45
  • Could you please explain, what are `inlining` and `factoring`? Could you please provide an example? – softshipper Nov 11 '17 at 20:58
  • 1
    Inlining is replacing a reference to a variable with the right-hand side of its definition. Factoring out means removing duplicate code by saving the result of an expression in a variable and replacing every instance of that expression with a reference to that variable. – Jasper-M Nov 11 '17 at 23:35