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.
Currently, given a
MessageField
and aMessage
/Builder
, in order to read or write field's value, we have to resort to theMessageField
instance as the coordinator of reading and writing of field's value, e.g. like this:or
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:
As a result we should additionally be able to use such a syntax for reading/writing field values:
and
Not sure if there are any pitfalls, but so far it looks fine for me.