Kotlin contracts - delegate construction to external fucntion


#1

Hi. I want to do something like this:

class Test(callable: ()->Unit //say somehow that callable will set all vals in class) {
val string: String
init {
callable()
}
}

is it possible with contracts?


#2

No, it’s not and it will never be.

First you would need to pass an instance of Test to callable but it still would not work. You would never be able to set private or protected fields, so I don’t think this will be supported.

That being said, right now contracts can only be used for top-level functions anyways.