Hello everyone.
I have found a piece of code that compiles correctly for all targets, but Java. An error is thrown during the native compilation, and I think is related to the classes hierarchy or the classes type parameters. This is the code:
class
Controller
{
public function new():Void {}
public function action():Void {
throw "implement me";
}
}
class
HomeController
extends
Controller
{
override public function action():Void {
trace("home sweet home");
}
}
class
ControllerBuilder<
TController:Controller
>
{
public function new():Void {}
public function build():TController {
throw "implement me";
}
}
class
HomeBuilder
extends
ControllerBuilder<
HomeController
>
{
override public function build():HomeController {
return new HomeController();
}
}
class
Route1<
TController:Controller
>
{
public function new():Void {}
public function buildController():TController {
throw "implement me";
}
}
class
Route2
extends
Route1<
Controller
>
{}
class
Route3<
TController:Controller,
TControllerBuilder:ControllerBuilder<
TController
>
>
extends
Route2
{
override public function buildController():TController {
var
controllerBuilder:TControllerBuilder = makeControllerBuilder(),
controller:TController = controllerBuilder.build()
;
return controller; // This is the problematic line
}
private function makeControllerBuilder():TControllerBuilder {
throw "implement me";
}
}
class
HomeRoute
extends
Route3<
HomeController,
HomeBuilder
>
{
override private function makeControllerBuilder():HomeBuilder {
return new HomeBuilder();
}
}
class Main {
static public function main():Void {
var
route:HomeRoute = new HomeRoute(),
controller:HomeController = route.buildController()
;
controller.action();
}
}
Three classes: Route, ControllerBuilder and Controller. On Route.buildController a ControllerBuilder is created and its build method gets called, where the Controller gets actually created.
Trying to compile that for the Java target using the Haxe Compiler 3.2.1 on Linux x64 throws this:
haxelib run hxjava hxjava_build.txt --haxe-version 3201 --feature-level 1
javac "-sourcepath" "src" "-d" "obj" "-g:none" "@cmd"
src/haxe/root/Route3.java:52: error: incompatible types
return controller;
^
required: Controller
found: TController
where TController is a type-variable:
TController extends Object declared in class Route3
1 error
Compilation error
Native compilation failed
Error: Build failed
Peeking into the generated Route3.java file I find this:
package haxe.root;
import haxe.root.*;
@SuppressWarnings(value={"rawtypes", "unchecked"})
public class Route3<TController, TControllerBuilder> extends haxe.root.Route2
{
...
@Override public haxe.root.Controller buildController()
{
//line 89 ".../Main.hx"
TControllerBuilder controllerBuilder = this.makeControllerBuilder();
//line 90 ".../Main.hx"
TController controller = ((TController) (haxe.lang.Runtime.callField(controllerBuilder, "build", null)) );
//line 92 ".../Main.hx"
return controller;
}
...
I guess that the problem is the returning type of the buildController function, that should be TController instead of haxe.root.Controller .
What do you think? Is the hierarchy chain weird or something? Is it a Java target bug?
Thanks!