Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin shortcut for assigning method parameter to a field in Android Studio

In Android Studio, while I am working on a Java class when i press Alt + Enter and assign method parameters to class member fields.

But when I use Kotlin there are no suggestions to this functionality.

Could I create a new suggestion for this or is there any way to assign parameters to the fields?

Java class suggestion:

java class suggestion

Kotlin class suggestion:

enter image description here

like image 600
FarshidABZ Avatar asked Dec 03 '25 20:12

FarshidABZ


1 Answers

In kotlin you don't have to write a setter method in order to do this, that's probably why you don't get the suggestion.

In your example, if you wanted to have an authPresenter field with getters and setters, you'd declare it as a var and kotlin provides the rest:

class AuthenticationView(...) {
    var authPresenter: AuthPresenter? = null
}

This gives us a nullable AuthPresenter. And the idiomatic way to call this:

val view = AuthenticationView(...)
view.authPresenter = AuthPresenter(...)

Under the covers, that call to .authPresenter = someObject is turned into setAuthPresenter(someObject).

If you were to call this from Java, you'd see the getAuthPresenter() and setAuthPresenter() automatically generated for you.

In short: the reason the reason the option isn't there for Kotlin, is because there is a more idiomatic way to do this.

like image 127
Todd Avatar answered Dec 05 '25 10:12

Todd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!