views:

317

answers:

1

I can already get at all of the functions in a class by doing something like the following.

for (var member in obj) {
    if (obj[member] instanceof Function) {
        var f:Function = obj[member];
        ...
    }
}

Is there a way to get at a function's parameter list in actionscript? For example, can I write a function that does something like this?

function getFunctionArguments (f:Function) : Array {
    var argumentArray:Array = new Array();
    for (...) {
        ...
        argumentArray.push({ name:<argumentName>, type:<argument type> });
    }
    return argumentArray;
}

If so, what do I fill in at the ellipses?

+1  A: 

Nosirree. I'd like to give you a workaround, but there's no way to introspect a function's signature like this.

What you can do is, when the function is actually called, inside it you can browse through the arguments irrespective of the signature, by looking into the arguments object. As in:

function doSomething() {
    if (arguments.length > 0) {
         if (typeof arguments[0] == "string") {
             ....
         }
    }
}

and so on. But even then there's no way to find out the name of the arguments in the function signature (and this works fine even if there are no arguments in the signature, as above).

fenomas
That's a pretty useful thing to know, but my motivation is to write some infrastructure to make my classes inspectable by debug code, so it needs to be something that you can do before getting into the function body.I'll leave the question open for a bit if someone else has any more clever tricks to contribute, but it looks like you're right.
fastcall
Yeah, I guessed the reason, I was just expostulating. What you're looking for is definitely not possible though, not in AS3 either. The only possible approach would be to inspect the binary content of the SWF itself (which would have to be done outside of flash), and even then I'm not 100% sure whether function arguments get their names in the string table or not.
fenomas