Is there a way to control class visibility, so the...
# multiplatform
c
Is there a way to control class visibility, so they can only be used within the shared code and not visible at all on the platforms themselves?
m
If you are following a modularization architecture, then I think you can define it as an internal class!
c
That was my thought as well, I just can’t quite wrap my head around it. It seems that whenever I try to use an internal class in a public class, then I get
public' function exposes its 'internal' parameter type
But it’s probably just missing knowledge on my end about modules and internal visibility - and not KMP related 🙂
m
You're right. This error occurs because
public
APIs can't expose
internal
types. To resolve this, ensure that all functions and classes using the
internal
class are also
internal
or
private
. Alternatively, use a
public
interface:
Copy code
// Internal class
internal class InternalClass {
    fun doSomething() {}
}

// Public interface
interface PublicInterface {
    fun doSomething()
}

// Public class using the internal class
public class PublicClass : PublicInterface {
    private val internalInstance = InternalClass()

    override fun doSomething() {
        internalInstance.doSomething()
    }
}
This keeps
InternalClass
hidden while exposing
PublicClass
. Hope this helps!
c
That’s interesting, I’ll look into it, thanks!
m
You're welcome! 🙂
c
In my case, I actually just had to mark the constructor as internal as well, that was the only place it was causing problems 🙂
👍 1