HaxeFoundation / HaxeFoundation/haxe
[cpp] Generic functions not resulting in optimally typed code
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
In HaxeFlixel, we have a class [FlxDestroyUtil.hx](https://github.com/gamedevsam/flixel/blob/generics_fail/flixel/util/FlxDestroyUtil.hx), it has the following function:
```
/**
* Checks if an object is not null and exists before calling destroy(), always returns null.
*
* @param object An IFlxExisting object that will be destroyed if it's not null and exists.
* @return null
*/
@:generic public static function destroyExisting(object:T):T
{
if (object != null && object.exists)
{
object.destroy();
}
return null;
}
```
That function, when called from [FlxLinkedList](https://github.com/gamedevsam/flixel/blob/generics_fail/flixel/system/FlxLinkedList.hx#L81) class (which implements IFlxExisting), results in the following cpp code:
```
::flixel::_system::FlxLinkedList FlxDestroyUtil_obj::destroyExisting_flixel_system_FlxLinkedList( ::flixel::_system::FlxLinkedList object){
HX_STACK_FRAME("flixel.util.FlxDestroyUtil","destroyExiting_flixel_system_FlxLinkedList",0xaa3cfb51,"flixel.util.FlxDestroyUtil.destroyExisting_flixel_system_FlxLinkedList","flixel/util/FlxDestroyUtil.hx",32,0xaf22421a)
HX_STACK_ARG(object,"object")
HX_STACK_LINE(33)
bool tmp = (object != null()); HX_STACK_VAR(tmp,"tmp");
HX_STACK_LINE(33)
bool tmp1; HX_STACK_VAR(tmp1,"tmp1");
HX_STACK_LINE(33)
if ((tmp)){
HX_STACK_LINE(33)
tmp1 = object->__Field(HX_HCSTRING("exists","\xdc","\x1d","\xe0","\xbf"), hx::paccDynamic );
}
else{
HX_STACK_LINE(33)
tmp1 = false;
}
HX_STACK_LINE(33)
if ((tmp1)){
HX_STACK_LINE(35)
object->__Field(HX_HCSTRING("destroy","\xfa","\x2c","\x86","\x24"), hx::paccDynamic )();
}
HX_STACK_LINE(37)
return null();
}
STATIC_HX_DEFINE_DYNAMIC_FUNC1(FlxDestroyUtil_obj,destroyExiting_flixel_system_FlxLinkedList,return )
```
It appears Haxe generated the correct type for the parameter `::flixel::_system::FlxLinkedList object`, but is still using `__Field` to access members of that object for some reason.
Contributor guide
Assessment
This issue has not been assessed yet.