Type inference for val x get() = "Something"


#1

I have an interface for a value which is always computed, so I don’t need a backing field. (I replaced the computed object with a string in the example.)

When I write it like this:

interface MyInter {
    val x: String
}

class Test : MyInter {
    override val x = "";
}

AndroidStudio/IDEA tells me that x has backing field, so I write:

class Test {
    override val x get() = "";
}

which works, but now Kotlin wants me to add a type-annotation.

Is there a specific reason for this and a way around it? I search the bugtracker, but couldn’t find anything.

Regards


#2

Here is the issue https://youtrack.jetbrains.com/issue/KT-550


#3

Another question about type inference, again I searched the tracker and couldn’t find it:

Is there a specific reason this not legal:

interface X {
    fun test()
}

val test: X = object{
    override fun test() {
        throw UnsupportedOperationException()
    }
}

#4

You doesn’t override anything in example above. You forgot to specify parent after object declaration.


#5

I have many nested interfaces in my application, so many override s. It would be cool if I could just write:

interface Outer {
    val inner: Inner;
}

interface Inner {
    val x: String;
}

class Test : Outer {
    override val inner = object {
        override val x = "something"
    }
}