Override named parameters with specific naming

I have a method such as

interface List<T> {
    add(item: T)

and I have a subclass such as

class CatsList() : List<Cat> {
    add(cat: Cat)

The compiler argues with warning that overriding item named argument with cat may cause problems if someone wishes to do: catList.add(item = meowth)

I believe it sometimes makes sense to change the named argument to a more specific name for your derived class. It would be nice to have an option to explicitly rename the args.

Here’s a runnable example:

open class Box {
    open fun put(item: Any) {}

class CatBox : Box() {
    override fun put(cat: Any) {}

fun main() {
    println("Hover over the ! icon to read warning")

I would suggest it is rare to name an argument with a more specific name without weakening your code. The meaning behind the argument’s name should likely be the same as the parent since it should be substitutable (LSP)

With that said, you do have the option to do this. It is only a warning and your code will compile. You can use Kotlin’s Suppress to drop the warning.

Try uncommenting the @suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") in the example above. The suppress can be on the method (like in the example), the class, or the file

fun foo() { /* only this method suppressed */ }

class Foo { /* the entire class suppressed */ }

/* everything in the file suppressed */
1 Like