Write-only property, or different types for getter and setter?


#1

It seems I can’t do this. Is there a way?

class MyBuilder {
    private val map = TreeMap<String, Any>()
    var body: String
        set(value) {
            map.put("body", value)
        }
}

val b = MyBuilder()
b.body = "foo"

I want to use property syntax to add things to the map. I don’t want a type of String? for setter, because if you add anything it should not be null. If I could have different types for get and set they could be String? and String.

Rob


#2

It’s hard to answer your question, because it hard to figure from your question what kind of problem you are trying to solve (there is a change that you are aiming for wrong solution). Please be more specific about the problem (not just what you would like to do in Kotlin, but in general)


#3

At this time Kotlin supports neither write-only properties nor parameter type overloading for the setter. It’s possible that either of those features will be added to a future version of Kotlin, even though they’re not on the 1.1 roadmap.


#4

A couple YouTrack issues to follow/+1 that are related to this: