chaquo / chaquopy

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

Automatically decide whether to convert Python list to `String[]` or `int[]` overload #1055

Open matan-h opened 6 months ago

matan-h commented 6 months ago

I want to execute this code (which fine work in kivy):

intent.putExtra(Intent.EXTRA_EMAIL, [recipient])

and I get this error:

W/python.stderr: TypeError: android.content.Intent.putExtra is ambiguous for arguments (str, list): 
options are 
android.content.Intent android.content.Intent.putExtra(java.lang.String, byte[]),
android.content.Intent android.content.Intent.putExtra(java.lang.String, char[]),
android.content.Intent android.content.Intent.putExtra(java.lang.String, double[]), 
android.content.Intent android.content.Intent.putExtra(java.lang.String, float[]), 
android.content.Intent android.content.Intent.putExtra(java.lang.String, int[]),
android.content.Intent android.content.Intent.putExtra(java.lang.String, long[]),
android.content.Intent android.content.Intent.putExtra(java.lang.String, android.os.Parcelable[]),
android.content.Intent android.content.Intent.putExtra(java.lang.String, java.lang.String[]),
android.content.Intent android.content.Intent.putExtra(java.lang.String, short[]), 
android.content.Intent android.content.Intent.putExtra(java.lang.String, boolean[])

and the jarray method is feeling like it just complicates the code and greatly affects the ease of use:

intent.putExtra(Intent.EXTRA_EMAIL, jarray(jString)([recipient))

Can chaquopy detect if a list has only str or only int and convert it?

mhsmith commented 6 months ago

Overload resolution is currently only based on the top-level type of the argument, not its value. It would be quite difficult to change this.

However, I agree the syntax could be improved (#1058).

matan-h commented 6 months ago

While I truly believe the syntax can be improved, this issue is more towards running kivy code in chaquopy. pyjnius library (e.g. from pyjnius import ... is quite straightforward to convert to from java import ....) However, this implementation difference it what preventing kivy code (without the kivy library, such as kvdroid), to archive 95% functionality with chaquopy.

matan-h commented 6 months ago

For demonstration proposes, I've created a way to run libraries written in Pyjnius. It's effectively the Pyjnius functions written using chaquopy java module. this library work for almost (~90%) all functions in plyer and kvdroid.tools that does not need list<->jarray conversion.

Most jcontainer->list cases (#1048) solved by simply adding the cross-framework function : .toArray. However, to solve the cases of list->jarray I'll need to add the chaquopy-spesific function jarray(type)(array).

mhsmith commented 6 months ago

I'm open to the idea of looking at the content of a list in situations like this, but it would be a complex change. The starting point would be the current implementation of the overload resolution algorithm.

Related: