Delegated extension properties for Map

Hello!

I’m in need of defining extension properties on Map and MutableMap interfaces, so that this properties will be readonly on Map interface and read write on MutableMap.

The only way I found to achieve this is follows:

val Map<String, String>.myProp: String by MyReadOnlyDelegate()

var MutableMap<String, String>.myProp: String by MyReadWriteDelegate()
    @JvmName("get_myProp2")  // this needed to resolve platform declaration clash

This is poor solution because I need to define same property twice and same getter twice.
Also if I will need to rename this property I must remember to rename both versions of it.

Is it possible to follow this logic in this special case?
1 If one define val extension property for Map or MutableMap, it should be accessible from both of them
2 If one define var extension property for MutableMap, its getter should also be accessible from immutable map reference(Map)

Thanks!

1 Like