Saturday, March 6, 2010

Blocks within if statements

his is another topic that examines the consistency of Scala. This topic examines blocks in if statements. It is related to Blocks within for comprehensions and Temporary Variables during object creation.
  1. // standard if
  2. if(1 > 2) -1 else 0
  3. // since blocks return a value you can use a block within the if statement 
  4. // (not sure when you would want to but...)
  5. if ({ val x = 1
  6.       val y = 2
  7.       x == y }) 1 else 2

2 comments:

  1. You forgot to remove REPL's | chars.

    ReplyDelete
  2. I need to add credits to all the helpers I have on this blog :)

    ReplyDelete