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

Justin

04/01/2019, 8:51 PM
Perhaps I’m mistaken or uninformed, but it does look like there are times when the Kotlin property syntax is misleading: e.g. I’m writing some code to cancel jobs via JobScheduler, and so in Kotlin I have the following code:
Copy code
val jobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as android.app.job.JobScheduler
jobScheduler.allPendingJobs
where
jobScheduler.allPendingJobs
is property syntax for the abstract method
JobScheduler#getAllPendingJobs
Initially I figured I’m just accessing a member variable of the
JobScheduler
(apparently abstract) class but when I dive into
JobScheduler
, I noticed that
allPendingJobs
is actually an abstract method, which, for all I know, is doing some intensive work and has not already done that work I guess what I’m asking is, why does Kotlin and/or the IDE use property syntax on abstract methods? or am I misunderstanding what “property syntax” means? or…? Thanks for your time!
hmmm, maybe this is actually more related to the Jetbrains IDE than to Kotlin - but even if that’s the case, then why is the IDE providing property syntax to an abstract method?
l

louiscad

04/01/2019, 9:34 PM
You should just use WorkManager instead of tricky JobScheduler (that will be deprecated)
1
j

Justin

04/01/2019, 11:15 PM
that was just my example of what I experienced to be misleading
g

gildor

04/01/2019, 11:57 PM
How abstract method syntax for Java getters is different from the same case with non abstract methods? Kotlin plugin by default uses property syntax for Java getters, just because it shorter and less noisy, overall I don't see any difference on usage of getAllPebdingJobs() and allPendingJobs in this case (except first is longer) This may concidered as violation of style guide about Property vs Function, but it mostly applied to Kotlin code, where developer decide, but in this case it is just convenient syntax, if you think that would be better to use getter instead, just use it (what you cannot do with Kotlin property)
3 Views