COMMUNITY

Hashlink native extension and function default value

My HL function declaration is like this:

@:hlNative("hlimgui")
class ImGui
{
	public static function setDisplaySize(display_width:Int, display_height:Int) {}
    ...

On the C side, the function is declared as this:

HL_PRIM void HL_NAME(set_display_size)(int display_width, int display_height)
{
    ...
}
DEFINE_PRIM(_VOID, set_display_size, _I32 _I32);

Now if I add a default value to the last parameter to the HL function:

	public static function setDisplaySize(display_width:Int, display_height:Int = 0) {}

The function signature changes and the last parameter becomes a reference to a Int, and I don’t see why. So I need to change the C function to be:

HL_PRIM void HL_NAME(set_display_size)(int display_width, int* display_height)
{
    ...
}
DEFINE_PRIM(_VOID, set_display_size, _I32 _REF(_I32));

This looks to me as a bug, but I may be wrong

Default parameters make the type nullable, your function is actually:
EDIT: Only in the generated code, in haxe it is treated as non nullable.

public static function setDisplaySize(display_width:Int, display_height:Null<Int>) {
  if (display_height == null) {
    display_height = 0;
  }
}

It’s a weird part of how haxe works, but that’s how default parameter are implemented.

@ibilon That’s incorrect, default values do not make types nullable. Only ? does.

function main() {
	$type(a); // (?i : Int) -> Void
	$type(b); // (?i : Null<Int>) -> Void
}

function a(i:Int = 0) {}
function b(?i:Int) {}

See also: https://stackoverflow.com/questions/31914614/meaning-of-question-mark-operator-before-arguments-in-haxe

Oh indeed not from the haxe side, I always confuse the two.
But in the generated code it is nullable, which you need to take into account for externs.