Templates: Default to expression body for new Kotlin functions
Answered
When overriding a method, is there a way to generated a kotlin-function as expression-body per default?
Right now
override fun xx() {
TODO()
}
is created, I would like to have
override fun xx() =
TODO()
by default.
Please sign in to leave a comment.
Please use https://discuss.kotlinlang.org/ for any questions related to Kotlin.
The question has nothing to do with language discussion. The author asks about IDEA feature when an overridden method shows up with some predefined body. Maybe some elaborations will help address this question. So, the author talks about the Idea setting "Settings -> Editor -> File and Code Templates -> New Kotlin Function Body". By default it fills a newly created method with the next value:
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
This predefined body may be changed in this editor:
So, after overriding the method will look like this:
However, the thing is that Kotlin allows to define a method in a short form ("expression-body"), so the method could also be defined as follows:
Although it's possible to change this default value in the template editor, the main problem is that there is no possibility to tell the editor that I want to omit those curly braces which the method's body is enclosed in. Therefore, it's not possible to set up a method's body filling in the "expression-body" style.
So, is there any way to add this functionality manually (perhaps, by editing some IDEA config files) or will it be added later?
IDEA parameters:
Request is welcome at https://youtrack.jetbrains.com/newIssue?project=KT. It still belongs to Kotlin support and IDE plug-in is also responsibility of the Kotlin team, so my comment regarding the proper place to report/discuss such issues is still valid.
Ok, thanks