-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Method arguments - inconsistent implicit conversion for boxed types #1098
Comments
The reason for the current situation is that we can't have int and float automatically go to a Java object as they don't have a defined size. A Python int is equal to short, integer, and long depending on the context while a Python float is both a float and double. For this reason, we need a cast so that it knows which conversion rules to apply.
The reason for this stems from the fact that the casting rules only have 4 levels of matching (exact, implicit, explicit, and none). If we place int and float on the boxed conversion list as implicit, it allows free matching including places such as Object conversions. I can look at it more closely, though last time I looked over boxed I determined that the side effects in terms of broken code would be too great to support automatic conversion from Python types to boxed. |
For reference in Java you can't call |
Yes, asking for |
I can give it a look after I get Python 3.11 version released, but if I remember the issue if I added that conversion then it allowed unfavorable conversions because JInt, JShort, and JLong are all derived from Python int. Thus adding an implicit conversion to a boxed types, opened all conversion rather than just the ones that were reasonable without an upgrade to the resolution rules. Perhaps I can add a check that only if it is exactly the Python type (not derived from) then it will convert implicitly. Though that would be rather odd from the OO as having something allowed for the parent and not the child is counter intuitive. |
Thanks! Would it be possible to do the same as what is currently done for primitive types: up-casting short -> int -> long is a allowed, but not vice versa:
|
I suspect that the second request is easier than the first. Looking at the source code each Primitive has its own wrapper type with its own type conversion table. Thus adding a new promotion as implicit to the primitive tables is just one line for each conversion. On the other hand boxed types just use the primitive table to figure out the conversions. I think part of the difficulty is coming from this structure. If I add JShort->JInt->JLong then you won't be able to use casting to select between java.lang.Short, java.lang.Integer, and java.lang.Long using the cast operators because the best match will be implicit and the cast up will also be implicit and thus it will start generating errors. We can break the boxed type wrapper up and add explicit tables for conversions which would allow the primitive types to promote properly which would add enough flexibility to make some of these conversions work without breaking the type tables. Though as it stands those tables are very linked. There is one alternative. I see that Boxed types have a hints conversion so we can add conversions using Python. Though that would be slower and thus not preferred. |
It appears the implicit conversion (for method arguments) handles boxed types inconsistently. For primitive types, implicit conversions do what I would expect:
int
from pythonint
long
from pythonint
double
from pythonfloat
or pythonint
However, for Boxed types this is not the case:
Integer
does not accept pythonint
Long
does not accept pythonint
Double
accepts pythonfloat
but not pythonint
.Minimal example:
The text was updated successfully, but these errors were encountered: