Last active
January 8, 2016 13:41
-
-
Save zmactep/582263bb877b6e4e2600 to your computer and use it in GitHub Desktop.
Funny bomb test on scala lazy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Вопросы: