haxe icon indicating copy to clipboard operation
haxe copied to clipboard

[js] It would be nice to have a more elegant output of default arguments in es6

Open R32 opened this issue 1 year ago • 4 comments

haxe source :

function multiply( a, b = 1 ) {
	return a * b;
}

Current output :

function multiply(a,b) {
	if(b == null) {
		b = 1;
	}
	return a * b;
}

Expected output : es6 default parameters

function multiply( a, b = 1 ) {
	return a * b;
}

R32 avatar Apr 14 '24 02:04 R32

Haxe often generates calls like multiply(1, null), so this will be impossible to do and will break the specification with null vars, because JS doesn't update null values in args

RblSb avatar Apr 14 '24 05:04 RblSb

I did some testing and found that it seems like Haxe only generates multiply(1, null) for external functions.

And also for external functions, null doesn't seem to be necessary in multiply(1, null). I know that null isn't undefined, but there should be some way to handle it.

R32 avatar Apr 14 '24 07:04 R32

final x = null; foo(x); also generate let x = null, and not undefined, so there is a lot of things to change to undefined generation, and it can break other things then

RblSb avatar Apr 14 '24 08:04 RblSb

In this case, we will have to change all null generation to undefined for js target. This could be more logical, but more significant reasons are needed than just this one case of pretty code, because this is still a breaking change for js.Lib.undefined checks

RblSb avatar Apr 14 '24 08:04 RblSb

IMO this isn't worth the trouble. There are likely subtle differences here which would cause problems down the line, so I prefer to keep things as they are.

Simn avatar Nov 19 '24 06:11 Simn