COMMUNITY

Return array from function

In my project, I’m using a static method to uncompress array data. I’m trying to return the array of integers that is built from the compressed data, but I’m not sure how to do it (or if it’s even possible). Is there a way to do this, and if not, is there any other way I could accomplish my goal?

Thanks for any help.

In Haxe you return an array just like you’d return any other object, for example:

class Main {
    public static function main() {
        trace(foo(4));
    }
    
    public static function foo(n : Int) {
        var a : Array<Int> = [];
        for (_ in 0 ... n) {
            a.push(Std.random(10));
        }
        return a;
    }
}

I kept trying to specify a return type in the function declaration, I guess old habits die hard.

Thank you.

What’s the problem though? This should work: public static function foo(n : Int) : Array<Int>

1 Like

Oh, you can of course still do that. I actually omitted it because I tend to lean on Haxe inferring it for me. :slight_smile: