Is it possible to use builder inference with varia...
# getting-started
j
Is it possible to use builder inference with variables? (See thread)
So I have this builder:
Copy code
class PostgresChangeBuilder<T : PostgresAction> {

    var schema: String = ""
    var table: String? = null
    var filter: String? = null
    lateinit var event: EventType<T>

    fun setEvent(event: EventType<T>) {
        this.event = event
    }

    fun buildConfig() = PostgresJoinConfig(schema.ifBlank { throw IllegalStateException("Schema not set") }, table, filter, event.name)

}
And this is my builder function:
Copy code
inline fun <reified T : PostgresAction> RealtimeChannel.postgrestChangeFlow(builder: PostgresChangeBuilder<T>.() -> Unit): Flow<T> { //T must be reified
    val postgrestBuilder = PostgresChangeBuilder<T>().apply(builder)
    //stuff 
}
Can I somehow only use the variable to make this work?
Copy code
val flow = channel.postgrestChangeFlow {
    setEvent(EventType.Select) //builder inference does work
    event = EventType.Insert //builder inference doesn't work
}
g
I found a bug thanks to your remark. (See the issue.) As for variable assignment, I see it currently as a great bug, because it does not work correctly at all (at least in my environment 😅). So the only workaround I see (until the bug is fixed) is to manually declare additional setters for the variables and use them in builder
Copy code
class PostgresChangeBuilder<T : PostgresAction> {
    fun changeSchema(value: String?) { schema = value }
    // ...
}

// later:

val flow = channel.postgrestChangeFlow {
    changeSchema("...")
}