Created
October 10, 2016 15:14
-
-
Save matklad/924aa8b328085fe952b0b819b2f5d3ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface I<SELF> { | |
fun base(): V | |
fun reversed(): SELF | |
infix fun add(other: SELF): SELF | |
} | |
abstract class Adapter<SELF : I<SELF>>(val base: V) : I<SELF> { | |
abstract fun ctor(base: V): SELF | |
final override fun reversed(): SELF = ctor(base.reversed()) | |
final override fun add(other: SELF): SELF = ctor(base.add(other.base())) | |
} | |
class V( | |
val coordinates: List<Int> | |
) { | |
fun reversed(): V = V(coordinates.reversed()) | |
infix fun add(other: V) = V(coordinates.zip(other.coordinates).map { it.first + it.second }) | |
} | |
class V2 private constructor( | |
val a: Adapter<V2> | |
) : I<V2> by a { | |
class A(base: V) : Adapter<V2>(base) { | |
override fun base(): V = base | |
override fun ctor(base: V): V2 = V2(A(base)) | |
} | |
constructor(x: Int, y: Int) : this(A(V(listOf(x, y)))) | |
} | |
class V3 private constructor( | |
val a: Adapter<V3> | |
) : I<V3> by a { | |
class A(base: V) : Adapter<V3>(base) { | |
override fun base(): V = base | |
override fun ctor(base: V): V3 = V3(A(base)) | |
} | |
constructor(x: Int, y: Int, z: Int) : this(A(V(listOf(x, y, z)))) | |
} | |
fun main(args: Array<String>) { | |
val x2 = V2(1, 2) | |
val y2 = V2(1, 2) | |
val z3 = V3(1, 2, 3) | |
x2 add y2 | |
x2 add z3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment