https://kotlinlang.org logo
#android
Title
# android
a

althaf

08/23/2022, 7:28 AM
what is the difference between these two code ?
Copy code
class VerifyAppIntegrityUseCase(
    val authRepository: AuthRepository,
    val workbenchUserRepository: WorkbenchUserRepository,
    val menuRepository: MenuRepository,
    val subscriptionRepository: SubscriptionRepository,
    val adkarRepository: AdkarRepository,
    val securedKeyValueStoreRepository: SecuredKeyValueStoreRepository,
 >>>   dispatcher: CoroutineDispatcher
) :
    AuthenticatedFlowUseCase<Unit, Unit>(authRepository, dispatcher), UseCaseScope
Copy code
class VerifyAppIntegrityUseCase(
    val authRepository: AuthRepository,
    val workbenchUserRepository: WorkbenchUserRepository,
    val menuRepository: MenuRepository,
    val subscriptionRepository: SubscriptionRepository,
    val adkarRepository: AdkarRepository,
    val securedKeyValueStoreRepository: SecuredKeyValueStoreRepository,
>>> val dispatcher: CoroutineDispatcher
) :
    AuthenticatedFlowUseCase<Unit, Unit>(authRepository, dispatcher), UseCaseScope
why would kotlin give such a dual option here, both options allow us to pass it to the Super class constructor
p

Paul Woitaschek

08/23/2022, 7:42 AM
Well if you have it as a val, you can also use it inside the class and access it from the outside
👀 1
c

Ciaran Sloan

08/23/2022, 9:11 AM
The difference here is how this Kotlin code compiles to Java. In the case of specifying
val
on the constructor property, you are defining this as a global variable in the class, which is instantiated in the constructor. In the case of not specifying
val
the property is not a global variable, but is just a constructor scoped property that can only be used when the instance of the class is being generated via the constructor.
🙌 3
3 Views