Should there be a way to make all interface method...
# language-evolution
s
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
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
But why can’t the compiler default functions with return type of Unit?
l
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
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
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
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
So this is really a thing that API author should consider, but seems like android didn't.
y
You can make an intermediary interface that defines defaults for those functions and just inherit from that intermediary interface instead
s
@Youssef Shoaib [MOD] That is an extremely cumbersome solution for a small issue.
y
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
.
344 Views