In the following code: ```internal class Outer { class Inner }``` is the `Inner` class `public` ...
l
In the following code:
Copy code
internal class Outer {
    class Inner
}
is the
Inner
class
public
or also
internal
?
What about `companion object`s?
Copy code
internal class MyClass {
    companion object {
        val myValue: Int
    }
}
Is the
companion object
and the
myValue
internal or public?
IntelliJ documentation on hover shows in both cases a
public
modifier
I.e.
public Inner
and
public companion object
, and also
public myValue
.
In the case of
Inner
, I would expect that it's really public, as it's not a member of
Outer
(it's just namespacing, it's not a Kotlin
inner class
)
For the
companion object
I'm not sure, conceptually it's also rather namespacing and not really a member of
MyClass
j
image.png
๐Ÿ™ 1
s
You might be overthinking it. Theyโ€™re public unless declared otherwise. But they are members of the class, so things that canโ€™t see the class canโ€™t see its members, regardless of their individual visibility.
๐Ÿ’ฏ 2
l
I just thought they weren't really "members"
j
They are members, the parent declaration of
Inner
is
Outer
๐Ÿ™‚
same goes for the companion object
l
Ok, that sounds much simpler than I thought ๐Ÿ™‚ Thank you for the explanation!
๐Ÿ‘ 1
j
however, you can make all the methods/properties/constructors of
Outer
internal
and keep
Inner
public
l
Yes, but it doesn't change anything as long as
Outer
itself is
internal
.
Then every member has also automatically
internal
visibility
(effectively)
j
Yeah, of course, you have to make the
Outer
class public. But since all its members (expect
Inner
) would be internal, it would essentially act as a namespace
l
Ah ok, yes ๐Ÿ‘
j
in the eyes of your user
tho, packages are probably more appropriate for that ๐Ÿ™‚
l
I'm thinking more in the direction of restricting access
Because I have a library where everything is public
And I'm trying to figure out, what I have to declare as
internal
to restrict the access as much as possible (i.e. to the actual current usage of the API; all client apps are also available to me)
j
FYI, you might find some interest in the explicit API mode: https://kotlinlang.org/docs/whatsnew14.html#explicit-api-mode-for-library-authors
l
Yes, unfortunately I'm on Android and there it will be working starting with 1.9.0
๐Ÿ˜ฉ 1
j
I didn't know about that limitation ๐Ÿ˜ฎ
at least, there is a workaround
l
It's not really feasible
Even with the workaround you will get the tests all red in the IDE
Thank you again for the quick help!
๐Ÿ‘ 1