Multiple Return Values in ActionScript

ActionScript has support for multiple return values through the use of objects or arrays. This feature can be used to return both result and error values from a function.

package {
    import flash.display.Sprite;
    import flash.text.TextField;

    public class MultipleReturnValues extends Sprite {
        public function MultipleReturnValues() {
            // The Function.call() method is used to call a function and
            // capture multiple return values in an array
            var result:Array = vals.call();
            
            var textField:TextField = new TextField();
            textField.text = result[0] + "\n" + result[1] + "\n" + result[1];
            addChild(textField);
        }

        // This function returns an Array with 2 int values
        private function vals():Array {
            return [3, 7];
        }
    }
}

In this example, we use an Array to return multiple values from the vals() function. The Function.call() method is used to invoke the function and capture its return values.

Here’s how we use the multiple return values:

var result:Array = vals.call();
trace(result[0]); // Outputs: 3
trace(result[1]); // Outputs: 7

If you only want a subset of the returned values, you can simply ignore the ones you don’t need:

var result:Array = vals.call();
trace(result[1]); // Outputs: 7

When you run this ActionScript code, it will output:

3
7
7

ActionScript doesn’t have a built-in way to ignore specific return values (like Go’s blank identifier _), but you can simply not use the values you don’t need.

While ActionScript doesn’t have native support for multiple return values like Go, using Arrays or custom objects can achieve similar functionality, allowing you to return and work with multiple values from a single function call.