HaxeFoundation / hxjava

Haxe Java support library. Build scripts and support code.
The Unlicense
46 stars 14 forks source link

Generics not written on function parameters #26

Closed kigero closed 5 years ago

kigero commented 5 years ago

I'm writing a utility function in Haxe that adds some information to an exception before it gets thrown, and I'm having an issue with this in Java because the type parameters aren't being written out to the java function.

I've got a function defined in haxe like this :

public static function ex<T:java.lang.Exception>(exClass:Class<T>, msg:String):T
{
    return Type.createInstance(exClass, [msg == null ? "" : msg]);
}

When this is compiled with the Java target, the java code that gets written looks like this:

public static <T> T ex(java.lang.Class exClass, java.lang.String msg)
{
    return ((T) (haxe.root.Type.createInstance(((java.lang.Class) (exClass) ), ((haxe.root.Array) (new haxe.root.Array(new java.lang.Object[]{( (( msg == null )) ? ("") : (msg) )})) ))) );
}

If I then try to use this function from java: throw ex(IllegalArgumentException.class, "test"); I get a compile error that "Object cannot be converted to Throwable". I believe this is because the type parameter is missing from the exClass parameter, so the java compiler just sets the return type to Object. If I add that type parameter to the exClass type:

public static <T> T ex2(java.lang.Class<T> exClass, java.lang.String msg)
{
    return ((T) (haxe.root.Type.createInstance(((java.lang.Class) (exClass) ), ((haxe.root.Array) (new haxe.root.Array(new java.lang.Object[]{( (( msg == null )) ? ("") : (msg) )})) ))) );
}

With this change the function compiles and works as expected. Any ideas if there is a workaround for this issue?

kigero commented 5 years ago

Closing here because it should have been reported elsewhere.

https://github.com/HaxeFoundation/haxe/issues/7749