Kotlin Using extensions with lambdas


Example

If you have:

operator fun <R> String.invoke(block: () -> R) = {
  try { block.invoke() }
  catch (e: AssertException) { System.err.println("$this\n${e.message}") }
}

You can write the following DSL-like code:

"it should return 2" {
   parse("1 + 1").buildAST().evaluate() shouldBe 2
}

If you feel confused with shouldBe above, see the example Infix approach to build DSL.