Please note that ExpatTech is close for the Christmas and New Year holidays. We will re-open on 2nd January 2024.

ExpatTech Techblog

Peter Todd 2008.11.28. 16:57

Flash Cookbook - Tricks - untyped function in Flash AS3

In this part we will see how functions work in actionscript 3.

Introduction

There are two types of functions. Either they return with something or not. Type "void" cannot return a type, otherwise functions can return a specific type (Number, String, Array, Sprite etc.). This is good. We should notice that flash doesn't have typed arrays at the moment. This means, if you give a return type "Array" to your function then you can make it return anything you want it to: just push the specific element to the Array. This is a good trick, but what happens, if (for some reason) you don't want it to return anything? This case is 1 from a million, but still.

The magic

There is a special way to use functions: give return type "*" to it. Let's contruct a universal function this way.

Function (1)

function univFunc(...args):*
{
     if(typeof(args[0]) == "number") return args[0] + 1;
     if(typeof(args[0]) == "string") return args[0].substr(0,2);
     if(args.length == 0) trace("no args");
}

You can see the following:

     1. function parameters are expressed with the phrase "...args". This way the number and type of parameters don't matter.

     2. we are choosing by the type of the first parameter.

     3. if there are no parameters then we do something else.

 Note that if you untype a function then you won't have any errors regarding the return type. The next functions cause errors at compiling:

Function (2)

function functionReturns(...args):String{}

Even though the next one returns a value, the return depends on the condition - an error is shown:

Function (3)

function functionReturns(...args):String
{
    if(typeof(args[0]) == "string") return args[0].substr(0,2);
}

Details

Let's have a look at Function (1). We call it with different types:

var variable:*;

variable = univFunc(1);
    trace(variable)
variable = univFunc("abc");
    trace(variable)
variable = univFunc();
    trace(variable)
variable = univFunc([1, 2, 3]);
    trace(variable)

OUTPUTS

2 - parameter was a number, it returned with a number

a - parameter was a string, it returned with the first character of it

no args -no parameters, trace function was made

undefined - traced the "return" of a no return

undefined - undefined, since we called our univFunc with an array and there was no declaration for such a case.

Conclusion

This way of using functions can be practical, becasue we can compress our code a bit. There can be cases, when it's easy to use it. For example we want the function to return with an object, if it can't then we want it to return with an errormessage. This way we can implement our message into the function and return a String instead of the specified object.

On the other hand a function like this can give the code an uncertain level of difficulty. Since it's untyped we can loose the clue about it's working, meaning and lifecycle.

So we like it, but advise you to use it carefully.