Skip to content

Static variables with default value do not have their typed expression processed by expression preprocessors #45

@Davvex87

Description

@Davvex87

Usually, class functions have their typed code expressions passed through a series of expression preprocessors to ensure that we output only the language features given by Haxe that our target language provides, if our language does not provide certain features then its the job for these preprocessors to translate the incompatible feature into a new expression that our language understands. Unfortunately, this does not seam to be the case for static class variables with a default starting value, our language compiler will simply print out whatever AST that expression provides without going through our preprocessors first, this can cause unintended behavior and even errors, not good!

Heres an example, if i have a function and add this code here, even if out target language does not support the way Haxe handles blocks and the "everything is an expression" principle then our preprocessors kick in and translate the code:

var myLocalVar =
    {
        if (untyped name == "abcd")
            5;
        else if(untyped name == "efgh")
            untyped brah(untyped name);
        else
            9 + 10;
    }

gets converted into:

var tempNumber

if (name == "abcd"):
    tempNumber = 5
else:
    if (name == "efgh"):
        tempNumber = brah.call(name)
    else:
        tempNumber = 9 + 10

var myLocalVar: int = tempNumber

however if we have a static variable like this:

static var myLocalVar =
    {
        if (untyped name == "abcd")
            5;
        else if(untyped name == "efgh")
            untyped brah(untyped name);
        else
            9 + 10;
    }

this is the output instead:

static var myStaticVar: int = if (name == "abcd"):
	5
else:
	if (name == "efgh"):
		brah.call(name)
	else:
		9 + 10

^^^ In GDScript, this is invalid syntax (Expected expression for variable initial value after "=".)

reflaxe.GDScript was used for this example, but this issue is persistent in reflaxe itself. I have tested and Haxe's own transpiler does account properly for this situation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions