[Solved] Returning arrays

Hi! Another noob question here! :sweat_smile:

I’d like to know if there’s a way to write a method that receives an array of any type, loops through it to perform operations and returns the same type. Or if isn’t possible with Haxe.


// Arrays with different types
var Test1:Array<Int> = [ 0, 1, 2, 3, 4 ];
var Test2:Array<String> = [ 'a', 'b', 'c', 'd', 'e' ];

// A method that accepts any type
public function arrayLoopOrWhatever( arrs:Array<Any> ):Array<Any> 
    // Perform whatever you need inside
    return arrs<Any>;
// Returned arrays with same type as original
Test1 = arrayLoopOrWhatever( Test1 );
Test2 = arrayLoopOrWhatever( Test2 );

I want to do this using something like a wildcard for type, without using Dynamic, is it possible?

Thanks. :blush:

Yes it’s possible.

public function arrayLoopOrWhatever<T>( arrs:Array<T> ):Array<T> 
    // Perform whatever you need inside
    return arrs;
1 Like

Thanks a lot, @ibilon! :smile: