Upcast exposed internal properties to supertype

I’m looking for a syntactic sugar or something else to facilitate exposing private/internal properties as an upcasted instance.
Suppose there exist a mutable array in a class:

class Person{
    private val _addressList = MutableList<String>()

If we need to expose _addressList then should write a function or another property which upcasts addressList as List<String>

fun getAddressList = _addressList as List<String>
// OR
val addressList = _addressList  as List<String>

Due to DRY principle, I’m curious to find a reusable way of exposing upcasted properties to avoid duplicating exposedProperty as List<String> everywhere in my code.

This has already been discussed here and there is an open issue for this (KT-14663).

At the moment, the second workaround:

val addressList: List<String> get() = _addressList

would (IMO) be more idiomatic than a function.

1 Like