Danilo Lima
03/23/2021, 10:30 PMJavier
03/23/2021, 10:42 PMephemient
03/23/2021, 11:01 PMclass Internal internal constructor() {
...
}
prevents external users from directly calling the constructor, allowing you to control access better (through a builder or factory method, perhaps)
this does the same thing for object
Danilo Lima
03/23/2021, 11:20 PMpablisco
03/24/2021, 9:42 AMsealed class PermissionResult {
object Granted : PermissionResult()
object Denied : PermissionResult()
}
And we get this from a suspend function in the same module:
suspend fun checkPermission() : PermissionResult
We wanted to create a function else where that can only be called if we have Granted
as a value:
fun doSomethingFun(granted: PermissionResult.Granted)
This doesn’t work as is since anyone can reference Granted
so by using internal implementations like this:
sealed class PermissionResult {
sealed class Granted : PermissionResult() {
internal object Internal : Granted()
}
sealed class Denied : PermissionResult() {
internal object Internal : Denied()
}
}
Means that you are forced to call the checkPermission
function before you can call doSomethingFun
which means it’s air tight and there is no way to forget to check the permission 🙂pablisco
03/24/2021, 10:36 AMDanilo Lima
03/24/2021, 12:58 PMpablisco
03/24/2021, 2:50 PM