https://kotlinlang.org logo
#language-evolution
Title
# language-evolution
s

Sam Stone

03/06/2023, 12:09 AM
Should there be a way to make all interface methods optional to implement? Take
android.text.TextWatcher
. I am not interested in
beforeTextChanged()
and
onTextChanged()
, only
afterTextChanged()
. Should the compiler assume (or can I inform it) that I will implement it like this?
Copy code
object : TextWatcher {
            override fun beforeTextChanged(...) {}
            override fun onTextChanged(...) {}
            override fun afterTextChanged(...) {
                ...
            }
        }
l

Loney Chou

03/06/2023, 12:23 AM
If you don't own this interface, you can only override them with an empty body. But if you do, you can add an empty body or return whatever the default value on their declaration site, in order to concrete them. I personally don't think defaulting them without explicit code is useful nor good practice since only functions with return type of Unit can do that.
s

Sam Stone

03/06/2023, 12:27 AM
But why can’t the compiler default functions with return type of Unit?
l

Loney Chou

03/06/2023, 12:31 AM
It can, but it's quite limited and cause inconsistency.
Sometimes some Unit functions must be implemented in order to properly function, but now you don't know that because of this compiler magic (compiler won't warn you).
r

Ruckus

03/06/2023, 4:07 PM
That also rather defeats the purpose of the interface. The whole point of the interface is to specify what shape you expect the implementation to take. A default implementation needs to be specified, not implied.
s

Sam Stone

03/06/2023, 7:00 PM
I am well aware of all of those points. I am suggesting something along the lines of an annotation which will generate default implementations for whatever I don’t. Or that the owner of the interface specify that the compiler should generate default implementations if the client doesn’t need the unimplemented functions (e.g. in action listeners like TextWatcher, where the client may not need to listen for every action, and writing an empty function, for each action is just needless boilerplate).
r

Ruckus

03/06/2023, 8:15 PM
Ah, my apologies. I misread and thought you were asking for this to be the default behavior.
the owner of the interface specify that the compiler should generate default implementations if the client doesn’t need the unimplemented functions
They can, by including a body in the interface.
Copy code
interface TextWatcher {
    fun beforeTextChanged(...) { /* Default do nothing implementation */ }
    ...
}
Is this not what you meant?
l

Loney Chou

03/06/2023, 11:58 PM
So this is really a thing that API author should consider, but seems like android didn't.
y

Youssef Shoaib [MOD]

03/07/2023, 5:18 AM
You can make an intermediary interface that defines defaults for those functions and just inherit from that intermediary interface instead
s

Sam Stone

03/09/2023, 12:02 AM
@Youssef Shoaib [MOD] That is an extremely cumbersome solution for a small issue.
y

Youssef Shoaib [MOD]

03/25/2023, 5:31 PM
If you have a lot of text watchers it will help though. Something as simple as:
Copy code
interface TextWatcherBase: TextWatcher {
            override fun beforeTextChanged(...) {}
            override fun onTextChanged(...) {}
            override fun afterTextChanged(...) {}
}
and you can inherit from it instead of
TestWatcher
.
232 Views