I am reading through the existing codebase for my team, and I notice mutableListOf
are always declared as val
. In some scenarios, elements are added to the mutableListOf
only once. E.g
val jobList = mutableListOf<JobActivity>()
jobList.addAll(job.activities)
In other scenarios, elements are added to mutableListOf
in a loop. E.g
val jobList = mutableListOf<JobActivity>()
newJobList.filterScanType(retrieveJobType(JobContext.NEW)).forEach {
jobList.add(it)
}
Since the list is not initialized on creation, why not declare mutableListOf
as var
? A lot of examples found online also follow the same pattern of declaring mutableListOf
as val
.
Which is best to use in the 2 scenarios described, val
or var
?