"xxx might be used before being initialized" warnings in Haxe 4.1

Hi

I just switched to 4.1.3 from 4.0.5 and suddenly some new warnings pop up. They are all related to using local functions.

Some example code (not useful, but for demonstration…)

function foo()
    {
        var loops = 0;
        var someRecursiveFunction:Int->Void;
        var start:Void->Void;

        someRecursiveFunction = function(value:Int)
        {
            if (value < 10)
                someRecursiveFunction(++value);
            else if (++loops < 5)
                start();
        }

        start = function()
        {
            var value = Std.int(Math.random() * 5);
            trace("staring with value", value);
            someRecursiveFunction(value);
        }

        start();
    }

The compiler will throw two warnings. The two function calls in the recursive function.

First one is the call to the “someRecursiveFunction(++value)”. This makes no sense as this is just a recursive call.
Second one is the “start()” call two lines beneath. “start” will always be defined, because the only situation where “someRecursiveFunction” is called, is in the “start” function itself.

The only way that the compiler isn’t complaining is this:

function foo()
    {
        var loops = 0;
        var start:Void->Void = null;

        function someRecursiveFunction(value:Int)
        {
            if (value < 10)
                someRecursiveFunction(++value);
            else if (++loops < 5)
                start();
        }

        start = function()
        {
            var value = Std.int(Math.random() * 5);
            trace("staring with value", value);
            someRecursiveFunction(value);
        }

        start();
    }

But I really don’t like it. Two different styles of defining local functions and the “= null;” is somehow useless…

Thanks :slight_smile:

But I really don’t like it. Two different styles of defining local functions and the “= null;”

Unfortunately it’s the only way to avoid warnings in your case.
Unless you promote those functions to class methods.

For more details on why this warning was introduced see Uninitialized variables not checked · Issue #7447 · HaxeFoundation/haxe · GitHub

Hm ok. Just checked the issue on Github. Not really happy with the descision you made there. Why throw a warning (or really an error later?) in sane code if the reason is a problem in the compiler? Adding a “= null;” just to have something initialized still feels wrong. But I can work around it :slight_smile:

Thanks for the feedback.