SpineEventEngine / Chords

UI components for Compose Multiplatform toolkit
Apache License 2.0
2 stars 0 forks source link

Codegen runtime: consider creating a message-centric field access API #41

Closed dpikhulya closed 3 weeks ago

dpikhulya commented 1 month ago

Currently, given a MessageField and a Message/Builder, in order to read or write field's value, we have to resort to the MessageField instance as the coordinator of reading and writing of field's value, e.g. like this:

val fieldValue = messageField.valueIn(message)

or

messageField.setValue(messageBuilder, fieldValue)

IMO, despite being justified techniically, such a style feels somewhat "inverted" for me as a user of the API, because a message actually owns a field (not the other way), so thanks Kotlin, it seems that we should be able to override the array-like access syntax to achieve a better syntax with just a couple of extra extensions like the following ones:

public operator fun <M: Message, V : MessageFieldValue> M.get(field: MessageField<M, V>): V =
    field.valueIn(this)

public operator fun <M: Message, V : MessageFieldValue> ValidatingBuilder<M>.set(
    field: MessageField<M, V>, value: V
): Unit = field.setValue(this, value)

As a result we should additionally be able to use such a syntax for reading/writing field values:

val fieldValue = message[messageField]

and

messageBuilder[messageField] = value

Not sure if there are any pitfalls, but so far it looks fine for me.