// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) package jwj interface I { fun m1() fun m2() } class C1 : I { override fun m1() { println("C1.m2") } override fun m2() { println("C1.m2") } } class C2(val real: I) : I by real { override fun m2() { println("Voila") real.m2() } } data class MyInt(val v: Int) { operator fun plus(o: MyInt) = MyInt(v + o.v) operator fun minus(o: MyInt) = MyInt(v - o.v) operator fun times(o: MyInt) = MyInt(v * o.v) operator fun div(o: MyInt) = MyInt(v / o.v) } class TenInt { val a = arrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) operator fun get(ix: Int) = a[ix] operator fun set(ix: Int, v: Int) { a[ix] = v } } fun Int.plusPowerOf2(n: Int): Int { return this + (1 shl n) } fun main(args: Array) { val ox = C2(C1()) ox.m1() ox.m2() val i1 = MyInt(1) val i2 = MyInt(2) val i3 = MyInt(3) val i7 = i3 * i2 + i1 println(i7) val ten = TenInt() println(ten[7]) ten[7] = 17 println(ten[7]) println(1.plusPowerOf2(0)) println(1.plusPowerOf2(1)) println(1.plusPowerOf2(2)) println(1.plusPowerOf2(3)) }