Underscore for unused parameters in functions

For lambdas we can use the underscore character for unused parameters which is very nice.

It would be also very nice to use this feature for normal function parameters:

fun(_ : Executor, query : RfidEntryQuery) {  }

Right now, if I try this syntax I get an error about ‘_’ is reserved for Kotlin which makes it possible to add this feature without much headache (I hope).

The use case that results in this is when I pass a function reference with ‘::’

fun attach(on : HTMLElement, callback : (event : Event) -> Unit)) {  }

fun setup() {
    attach(myElement, ::handler)
}

fun handler(event : Event) {
}

If I don’t use the event in handler I get a warning which is actually useful.

However, to remove the warning, I have to add an annotation which has it’s own problems:

  • depending where the programmer puts the suppress may lead to future errors (to wide suppress means possible problems remain hidden)
  • it hurts readability

It looks kind of similar to the ticket I posted before: KT-17746.

1 Like

Yes, it is basically the same.

The application of this enhancement would be much broader than the title of that ticket indicates, which might result in lower prioritization than it warrants. See my comment on the ticket, and please consider renaming/rephrasing the problem more broadly in order to get the attention of the product dev team.

In that case you should first provide some examples how you would imagine your usecases in a code. I couldn’t quite catch what you wanted to achieve from your comment alone.

I have problem understanding your comment as well. Do you mean something like this?

fun main() {
    getData(::filterOdd)
}

fun getData(filter: (Int, String) -> Boolean): List<String> {
    val list: List<String> = ...
    return list.filterIndexed(filter)
}

fun filterOdd(index: Int, _: String) = index % 2 == 1

Yes, this is another case where we are forced to accept some parameters by a function even if it doesn’t actually need them. But I wouldn’t say this broadens the use. I believe people rarely pass function references like this.