Access function before calling superclass constructor in Kotlin data class

You can only use member functions on a fully constructed objects. One way to work around that is to use private extension function or simply a function to fetch name:

private fun Context.fetchName(): String {
    ///...
    return cursor.getString(1)
}

data class DataObject(val id: Int, val name: String) {
    constructor(context: Context, id: Int) : this(id, context.fetchName())
}

Although I do think that using Cursor is a bit too heavy job for constructor. I'd use separate Factory like so:

data class DataObject(val id: Int, val name: String) {
    object FromCursorFactory {
        fun create(id: Int, context: Context): DataObject {
            val name = context.fetchName()
            return DataObject(id, name)
        }
    }
}

Further reading:

  • Is doing a lot in constructors bad?
  • Why is it considered bad practice to call a method from within a constructor?