HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.03k stars 648 forks source link

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

Open R32 opened 2 months ago

R32 commented 2 months ago

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;
}
RblSb commented 2 months ago

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

R32 commented 2 months ago

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.

RblSb commented 2 months ago

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 commented 2 months ago

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