Anki-Android-Backend icon indicating copy to clipboard operation
Anki-Android-Backend copied to clipboard

Lists should be exposed to Kotlin as `List`, not `MutableList`

Open david-allison opened this issue 2 years ago • 4 comments

Backend Codegen is problematic: it exposes a java.util.List, but this is actually a com.google.protobuf.Internal.ProtobufList. ProtobufList throws on mutation

Extends List to add the capability to make the list immutable and inspect if it is modifiable.

https://protobuf.dev/reference/java/api-docs/com/google/protobuf/Internal.ProtobufList

Reported in: https://github.com/ankidroid/Anki-Android/pull/14948

david-allison avatar Dec 11 '23 23:12 david-allison

I've had a quick Google, but no obvious annotations came up to tell the kotlin compiler that the Java list should be List<T> rather than MutableList<T> when accessed by Kotlin

If it means we need to port our codegen to Kotlin, this is likely not worthwhile

david-allison avatar Dec 11 '23 23:12 david-allison

Where are you seeing mutable? I looked in the generated Note.java, and it's a plain list:

  /**
   * <code>repeated string fields = 7;</code>
   * @return A list containing the fields.
   */
  @java.lang.Override
  public java.util.List<java.lang.String> getFieldsList() {
    return fields_;
  }

dae avatar Dec 12 '23 00:12 dae

Sorry, ignore me - I assumed that List was always an immutable variant in Java, but apparently that's not the case.

dae avatar Dec 12 '23 00:12 dae

And this is not our codegen by the way - it's the output of protoc.

dae avatar Dec 12 '23 00:12 dae