"Extract Field" in Kotlin don't work as expected
Answered
I often use "Extract Field" (https://www.jetbrains.com/help/idea/extract-field.html) to declare member variable in Java, but this is not work in Kotlin as expected:
Before:
class Class {
fun method() {
val a = 1
val b = 2
var c = a + b
}
}
fun method() {
val a = 1
val b = 2
var c = a + b
}
}
When I select "c" and "Extract Field", IDEA prompts "cannot introduce property for this expression". I assumed it should goes:
class Class {
var c1: String? = null
lateinit var c2: String
var c3 by Delegates.notNull<String>()
fun method() {
val a = "a"
val b = "b"
c1 = "$a $b"
c2 = "$a $b"
c3 = "$a $b"
}
}
var c1: String? = null
lateinit var c2: String
var c3 by Delegates.notNull<String>()
fun method() {
val a = "a"
val b = "b"
c1 = "$a $b"
c2 = "$a $b"
c3 = "$a $b"
}
}
Is there any way to introduce an expression to a member variable using a built-in refactor function provided by IDEA when using Kotlin as language?
Please sign in to leave a comment.
Hello, Phoenix!
Would you please use Kotlin forum at https://discuss.kotlinlang.org/c/support for Kotlin-related issues/questions. Thank you!
Thank you for your reply, but I think this is a function support issue for IDEA.
The " extract field" is a shortcut key for IDEA. It can well support Java but cannot support Kotlin.
I don't know whether this is a bug or an expected feature.
The mentioned feature in IDEA editor is a Kotlin-specific and is provided by the Kotlin plugin. Please use mentioned forum for this. Thank you.
Thank you,I see.Should I delete this post?
It is not necessary. You could post here the link to the created on Kotlin forum thread just for future references. Thanks!
Do you mean this plugin? https://plugins.jetbrains.com/plugin/6954-kotlin/ If so I'm still not seeing this working.
Please use https://discuss.kotlinlang.org/ for any Kotlin related questions.
Will do. Looks like it already got posted here https://discuss.kotlinlang.org/t/extract-property-in-kotlin-dont-work-as-expected/10458/2