vendredi 31 mars 2017

If-else without else causes recursive function to repeat

Why does the following produce the output "ayyayy" and not just "ayy" (once)?

def iftest(b: Boolean): Unit = {
  if(b) iftest(false)
  print("ayy")
}

I run it in REPL as iftest(true) which should cause it to fail on the first pass but succeed on the second (hence only one "ayy"). So why does it act like both succeed?

Is there some sort of recursive "backfolding" in scala that I don't know about?

Aucun commentaire:

Enregistrer un commentaire