chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
816 stars 131 forks source link

Add typed versions of PyObject container views #1207

Open mhsmith opened 3 months ago

mhsmith commented 3 months ago

This would be particularly helpful in avoiding the need to call fromJava when modifying the container. For example, see the os.environ example on the Java API documentation page.

API could be something like List<T> asList(Class<T> elementType) -- see note at PyList.set.

mhsmith commented 3 months ago

It's worse in Kotlin because the Map get/remove/containsKey functions, which apparently used to accept any object like Java, now appear to be fully type-safe. For example, attempting to call pyobject.asMap().containsKey("string") fails to compile with a type inference error, and the IDE shows it's resolved to an extension function whose purpose is unclear to me.

Ideally we'd like to be able to change this (from Electron Cash):

val iMap = i.asMap()
iMap[fromJava("address")] = fromJava(iMap[fromJava("address")].toString())

To something like this:

val iMap = i.asMap(String::class, Any::class)
iMap["address"] = iMap["address"].toString()

Also consider how element nullability could be expressed, e.g. SendContactsDialog.contacts in Electron Cash.