https://kotlinlang.org logo
#language-proposals
Title
# language-proposals
d

Davio

03/28/2023, 10:51 AM
Hello everyone, I was thinking about a certain feature which does not exist, partial implementation of interface functions with optional parameters, for instance this example:
Copy code
interface Foo {
    fun optionalParams(one: String, two: String? = null)
}

class BarImpl: Foo {
    override fun optionalParams(one: String, two: String?) {
    }
}

class FooImpl : Foo {
    override fun optionalParams(one: String) {
    }
}
This does not compile because FooImpl does not override the function correctly, it needs the second parameter, even if you don't use it inside of the function. Would it be possible to add a language feature like this? And would it be a useful idea?
d

dmitriy.novozhilov

03/28/2023, 10:54 AM
What is a usecase for this feature? In which situations it can help?
r

Ruckus

03/28/2023, 2:52 PM
How would you expect this to resolve?
Copy code
val foo: Foo = FooImpl()
foo.optionalParams("one", "two")
1
y

Youssef Shoaib [MOD]

03/28/2023, 3:47 PM
Maybe the compiler could support
_
for unused parameters? That would be a lot more general than just having this for optional parameters. I'm imagining something like this:
Copy code
class FooImpl : Foo {
    override fun optionalParams(one: String, _) {
    }
}
or if type specification is absolutely needed:
Copy code
class FooImpl : Foo {
    override fun optionalParams(one: String, _: String?) {
    }
}
That would be analogous to the usage of
_
in lambdas and restructuring declarations
d

Davio

03/28/2023, 4:24 PM
My use case was with various parameters which were part of a search request and, depending on the context, some mattered and some don't, but I recognized I could just bunch together the parameters in a data class and solve it that way. But the idea still intrigued me
It could be resolved because the compiled byte code does have that parameter, but it would just be ignored I guess
11 Views