Scala: custom control structures with several code blocks

Posted by Vilius Normantas on Stack Overflow See other posts from Stack Overflow or by Vilius Normantas
Published on 2011-01-01T08:43:21Z Indexed on 2011/01/01 8:53 UTC
Read the original article Hit count: 312

Filed under:
|

Is it possible to create a custom control structure with several code blocks, in the fashion of before { block1 } then { block2 } finally { block3 }? The question is about the sugar part only - I know the functionality can be easily achieved by passing the three blocks to a method, like doInSequence(block1, block2, block3).

A real life example. For my testing utilities I'd like to create a structure like this:

getTime(1000) {
  // Stuff I want to repeat 1000 times.
} after { (n, t) => 
  println("Average time: " + t / n)
}

© Stack Overflow or respective owner

Related posts about scala

Related posts about control-structure