'createDelegate' operator convention:

- resolution & inference
- additional diagnostic for language feature support
This commit is contained in:
Dmitry Petrov
2016-12-05 14:20:37 +03:00
committed by Stanislav Erokhin
parent d3ab0f066e
commit e2ba288323
14 changed files with 365 additions and 93 deletions
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
class Cell<out V>(val value: V)
class GenericDelegate<V>(val value: V)
operator fun <T> T.createDelegate(a: Any?, p: Any?) = GenericDelegate(this)
operator fun <W> GenericDelegate<W>.getValue(a: Any?, p: Any?) = Cell(value)
val test1: Cell<String> by "OK"
val test2: Cell<Any> by "OK"
val test3 by "OK"
@@ -0,0 +1,23 @@
package
public val test1: Cell<kotlin.String>
public val test2: Cell<kotlin.Any>
public val test3: Cell<kotlin.String>
public operator fun </*0*/ T> T.createDelegate(/*0*/ a: kotlin.Any?, /*1*/ p: kotlin.Any?): GenericDelegate<T>
public operator fun </*0*/ W> GenericDelegate<W>.getValue(/*0*/ a: kotlin.Any?, /*1*/ p: kotlin.Any?): Cell<W>
public final class Cell</*0*/ out V> {
public constructor Cell</*0*/ out V>(/*0*/ value: V)
public final val value: V
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class GenericDelegate</*0*/ V> {
public constructor GenericDelegate</*0*/ V>(/*0*/ value: V)
public final val value: V
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,17 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
class StringDelegate(val s: String) {
operator fun getValue(a: Any?, p: KProperty<*>): Int = 42
}
// NB no operator
fun String.createDelegate(a: Any?, p: KProperty<*>) = StringDelegate(this)
operator fun String.getValue(a: Any?, p: KProperty<*>) = this
val test1: String by "OK"
val test2: Int by <!DELEGATE_SPECIAL_FUNCTION_RETURN_TYPE_MISMATCH!>"OK"<!>
val test3 by "OK"
@@ -0,0 +1,16 @@
package
public val test1: kotlin.String
public val test2: kotlin.Int
public val test3: kotlin.String
public fun kotlin.String.createDelegate(/*0*/ a: kotlin.Any?, /*1*/ p: kotlin.reflect.KProperty<*>): StringDelegate
public operator fun kotlin.String.getValue(/*0*/ a: kotlin.Any?, /*1*/ p: kotlin.reflect.KProperty<*>): kotlin.String
public final class StringDelegate {
public constructor StringDelegate(/*0*/ s: kotlin.String)
public final val s: kotlin.String
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun getValue(/*0*/ a: kotlin.Any?, /*1*/ p: kotlin.reflect.KProperty<*>): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,10 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
operator fun String.createDelegate(a: Any?, p: KProperty<*>) = this
operator fun String.getValue(a: Any?, p: KProperty<*>) = this
val test1: String by "OK"
val test2 by "OK"
@@ -0,0 +1,6 @@
package
public val test1: kotlin.String
public val test2: kotlin.String
public operator fun kotlin.String.createDelegate(/*0*/ a: kotlin.Any?, /*1*/ p: kotlin.reflect.KProperty<*>): kotlin.String
public operator fun kotlin.String.getValue(/*0*/ a: kotlin.Any?, /*1*/ p: kotlin.reflect.KProperty<*>): kotlin.String
@@ -0,0 +1,14 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !LANGUAGE: -OperatorCreateDelegate
class WrongDelegate(val x: Int) {
operator fun getValue(thisRef: Any?, prop: Any): Int = x
}
<!UNSUPPORTED_FEATURE(only available since Kotlin 1.1: operator createDelegate)!>operator<!> fun String.createDelegate(thisRef: Any?, prop: Any) = WrongDelegate(this.length)
operator fun String.getValue(thisRef: Any?, prop: Any) = this
val test1: String by "OK"
val test2: Int by <!DELEGATE_SPECIAL_FUNCTION_RETURN_TYPE_MISMATCH!>"OK"<!>
val test3 by "OK"
@@ -0,0 +1,16 @@
package
public val test1: kotlin.String
public val test2: kotlin.Int
public val test3: kotlin.String
public operator fun kotlin.String.createDelegate(/*0*/ thisRef: kotlin.Any?, /*1*/ prop: kotlin.Any): WrongDelegate
public operator fun kotlin.String.getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ prop: kotlin.Any): kotlin.String
public final class WrongDelegate {
public constructor WrongDelegate(/*0*/ x: kotlin.Int)
public final val x: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ prop: kotlin.Any): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}