Files
kotlin/compiler/testData/codegen/box/classes/overloadUnaryOperator.kt
2016-11-16 18:47:42 +03:00

28 lines
586 B
Kotlin
Vendored

// WITH_RUNTIME
class ArrayWrapper<T>() {
val contents = ArrayList<T>()
fun add(item: T) {
contents.add(item)
}
operator fun unaryMinus(): ArrayWrapper<T> {
val result = ArrayWrapper<T>()
result.contents.addAll(contents)
result.contents.reverse()
return result
}
operator fun get(index: Int): T {
return contents.get(index)!!
}
}
fun box(): String {
val v1 = ArrayWrapper<String>()
v1.add("foo")
v1.add("bar")
val v2 = -v1
return if (v2[0] == "bar" && v2[1] == "foo") "OK" else "fail"
}