```interface Store { val users: List<String...
# getting-started
u
Copy code
interface Store {
    val users: List<String>
}

class StoreImpl : Store {
    private val mutableUsers = mutableListOf<String>()

    override val users: List<String>
        get() = mutableUsers

    fun addUser(name: String) {
        mutableUsers += name
    }
}
can I use explicit backing fields here somehow? i.e. when the property is defined on the interface
y
I think it's just:
Copy code
class StoreImpl : Store {
  override val users: List<String>
    field = mutableListOf()

  fun addUser(name: String) {
    users += name
  }
}
I believe there's an IDE action that does this? If not, there definitely should be!
u
it complains about the proeprty nit being final
e
Copy code
final override val users: List<String>
❤️ 1
y
It seems to work just fine on the playground even without
final
l
@Youssef Shoaib [MOD] won't that recreate the list for every call to
addUser
? I.e. it won't actually mutate the list because the exposed type is just
List
.
y
I believe
MutableList.plusAssign
specifically calls
.add
. you can also call
.add
yourself
l
@Youssef Shoaib [MOD] right, but in this case,
users
is declared as
List
, not
MutableList
.
u
no its mutable list, thats the whole point of the feature
y
It's also a
val
, so
+=
wouldn't work! The whole point is we're using the new explicit backing field feature, which lets you access the real type of the field inside the class, but hide it from the outside
u
btw the
final
getting grayed out is IDE being old? (latest AS)