https://kotlinlang.org logo
#announcements
Title
# announcements
h

Hullaballoonatic

07/10/2019, 3:16 AM
Why shouldn't we be able to set the visibility of interface fields and methods? It would be nice for things like this:
Copy code
interface Foo {
    var recentBar: Int

    protected val onBar: (Int) -> Int

    fun bar(x: Int): Int {
        recentBar = onBar(x)
        return recentBar
    }
}
I don't intend this as a suggestion, per say, hence why posting here. I assume there's an important (paradigmatic?) reason I'm missing for why such a suggestion is a terrible idea.
Copy code
All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.
Copy code
All constant values defined in an interface are implicitly public, static, and final
h

Hullaballoonatic

07/10/2019, 3:24 AM
I presume you're implying that JB would be unable to make Kotlin's interfaces have this feature because the jvm requires public visibility?
d

Dariusz Kuc

07/10/2019, 3:24 AM
it would break Java compatibility
h

Hullaballoonatic

07/10/2019, 3:25 AM
Righto. But then my question simply becomes: Why doesn't Java allow for this?
d

Dariusz Kuc

07/10/2019, 3:25 AM
also interfaces are meant to define public APIs
j

jw

07/10/2019, 3:25 AM
Java allows public or private. The other two visibilities undermine the concept of an interface, and you should just make a second interface with reduced visibility in that case.
👆 3
e

epabst

07/10/2019, 4:57 AM
FWIW (probably obvious), if you don't need multiple inheritance, you could make Foo be an abstract class and do all of these things.
👆 1
p

Paulius Ruminas

07/10/2019, 5:43 AM
Interfaces were designed to describe the contract of the class not implementation details that's why there is only public modifier. If you want to have implementation details use an abstract class for that reason.
👆 4
a

Al Warren

07/10/2019, 2:08 PM
I was under the impression interfaces were designed to prevent multiple inheritance and the diamond of death. 😁
h

Hullaballoonatic

07/10/2019, 2:55 PM
But muh compositions
Thanks for the replies, everyone. As suspected, I'm a dirty cheater.
p

Paulius Ruminas

07/10/2019, 4:49 PM
I was under the impression interfaces were designed to prevent multiple inheritance and the diamond of death.
True but do interfaces really solve those problems? 🙂
h

Hullaballoonatic

07/10/2019, 4:52 PM
I just see a lot of potential for cool compositional programming with kotlin's interfaces. With delegation, I almost have multiple inheritance already... D:
a

Al Warren

07/10/2019, 5:04 PM
As I understand, it was the addition of default methods that was supposed to solve the multiple inheritance risk.
h

Hullaballoonatic

07/10/2019, 5:06 PM
Yeah and that just makes me hunger for default field values. I've tasted blood, and now I desire the forbidden!