karelpeeters
10/28/2019, 10:14 AMA
to instantiate a class B
, while keeping B
itself public? Basically I want B
to have a private constructor that can also be accessed by A
but not by a random class C
.
Putting A
and B
is the same file doesn't allow A
to use the private constructor, and even making B
a nested class of A
also doesn't work. Is there a solution?Dias
10/28/2019, 10:27 AMkarelpeeters
10/28/2019, 10:29 AMclass A {
fun foo() = B()
class B private constructor()
}
class C {
fun bar() = A.B()
}
I want foo
to compile and bar
to not compile.Dias
10/28/2019, 10:29 AMkarelpeeters
10/28/2019, 10:30 AMnested
keyword in Kotlin. In Java there is "nothing" for inner, static
for nested, in Kotlin it's inner
for inner and "nothing" for nested.Dias
10/28/2019, 10:31 AMclass A {
val b = B()
inner class B {
fun test(){
}
}
}
class C {
val a = A()
val b = A.B()
fun test() {
a.b.test()
}
}
class C doesn't compile herekarelpeeters
10/28/2019, 10:32 AMDico
10/28/2019, 12:04 PMkarelpeeters
10/28/2019, 12:05 PMDico
10/28/2019, 12:07 PMkarelpeeters
10/28/2019, 12:08 PMPablichjenkov
10/28/2019, 12:21 PMDias
10/28/2019, 12:26 PMinternal
? 🙂Pablichjenkov
10/28/2019, 12:32 PMfriend classes
in c++ over package private.