I have the following Java and Kotlin classes:
package nl.friesoft.solaredgenotifier;
class SolarEdgeException extends Exception {
public SolarEdgeException(String s) {
super(s);
}
}
package nl.friesoft.solaredgenotifier
class SiteStorage(context: Context): ISolarEdgeListener {
override fun onError(site: Site?, exception: SolarEdgeException?) {
}
}
And I get this compilation error:
'public' function exposes its 'public/package/' parameter type SolarEdgeException
Both classes are in the same package, so why doesn't this compile?
Classes by default in Kotlin are
public, so every member/function is exposed to the outer world. Contrarily, in Java the default visibility, that is omitting a visibility keyword such aspublic,protectedorprivate, ispackage-private.SolarEdgeExceptionispackage-private, while its userSiteStorageis entirelypublic.That means a user of
SiteStoragecannot catch or use your exception at all.Unfortunately Kotlin doesn't have the concept of
package-private, because packages aren't managed the same way as in Java.The best you can have is
internal, which means the definition is visibile to the entire module.I'd say, in this case, make
SolarEdgeExceptionapublicclass, maintaining the constructor aspackage-private.There is a forum thread on the
package-privatevisibility for Kotlin, it's an interesting read.kotlin-to-support-package-protected-visibility