Tutorial by Examples

If you have: infix fun <T> T?.shouldBe(expected: T?) = assertEquals(expected, this) you can write the following DSL-like code in your tests: @Test fun test() { 100.plusOne() shouldBe 101 }
If you have: class MyExample(val i: Int) { operator fun <R> invoke(block: MyExample.() -> R) = block() fun Int.bigger() = this > i } you can write the following DSL-like code in your production code: fun main2(args: Array<String>) { val ex = MyExample(233) ex ...
If you have: val r = Random(233) infix inline operator fun Int.rem(block: () -> Unit) { if (r.nextInt(100) < this) block() } You can write the following DSL-like code: 20 % { println("The possibility you see this message is 20%") }
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").bu...

Page 1 of 1