Skip to content

Instantly share code, notes, and snippets.

@zmactep
Last active January 8, 2016 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zmactep/582263bb877b6e4e2600 to your computer and use it in GitHub Desktop.
Save zmactep/582263bb877b6e4e2600 to your computer and use it in GitHub Desktop.
Funny bomb test on scala lazy
sealed trait BombLike {
def power : Int
def name : String
}
case class Bomb(power : Int, name : String) extends BombLike {
println("Explosion!")
}
case object Clock extends BombLike {
override def power : Int = 0
override def name : String = "clock"
println("Alarm!")
}
def process(a : => Option[BombLike]) : Unit = {
printWhat {
a match {
case Some(bl) => bl
case None => Clock
}
}
}
def printWhat(a : => BombLike) : Unit = {
println(runInSafe(a))
}
def runInSafe(bl : => BombLike) : String = {
println("Start of safe zone")
val result = s"${bl.name} of power ${bl.power}"
println("End of safe zone")
result
}
process(Some(Bomb(100500, "Little bomb")))
@zmactep
Copy link
Author

zmactep commented Dec 30, 2015

Вопросы:

  1. Что произойдет: взрыв или сигнал будильника?
  2. Сколько раз произойдет это событие? Почему?
  3. Событие произойдет в безопасной зоне или вне ее? Почему?
  4. Как переписать код так, чтоб событие произошло ровно один раз, но при этом осталось в безопасной зоне?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment