Has anyone ever heard of a way to dynamically retrieve a function’s signature? Example:
ActionScript Code:
[FONT=Courier New][LEFT][COLOR=#000000]function[/COLOR] tester [COLOR=#000000]([/COLOR]p0:[COLOR=#0000FF]String[/COLOR], p1:[COLOR=#0000FF]Number[/COLOR], p2:[COLOR=#0000FF]Number[/COLOR], p3:[COLOR=#0000FF]MovieClip[/COLOR] …[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#808080]//do stuff[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]function[/COLOR] getSignature COLOR=#000000[/COLOR]:[COLOR=#0000FF]Array[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000]var[/COLOR] parameter_array:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000]new[/COLOR] [COLOR=#0000FF]Array[/COLOR] COLOR=#000000[/COLOR];
[COLOR=#808080]//look at the parameter typeof requirements for tester ()[/COLOR]
[COLOR=#808080]//build an array like using some sort of for in loop:[/COLOR]
parameter_array[COLOR=#000000][[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]][/COLOR] = [COLOR=#FF0000]“String”[/COLOR]
parameter_array[COLOR=#000000][[/COLOR][COLOR=#000080]1[/COLOR][COLOR=#000000]][/COLOR] = [COLOR=#FF0000]“Number”[/COLOR]
parameter_array[COLOR=#000000][[/COLOR][COLOR=#000080]2[/COLOR][COLOR=#000000]][/COLOR] = [COLOR=#FF0000]“Number”[/COLOR]
parameter_array[COLOR=#000000][[/COLOR][COLOR=#000080]3[/COLOR][COLOR=#000000]][/COLOR] = [COLOR=#FF0000]“MovieClip”[/COLOR]
[COLOR=#0000FF]return[/COLOR] parameter_array;
[COLOR=#000000]}[/COLOR]
[/LEFT]
[/FONT]
I’m not familiar with the term function’s signature. Does that mean the data type of a variable (String,Number,etc) ?
:cowboy:
wheres The Canadian when you need him?
Dunno what you realy mean… There are two things in As that you mich be looking for… typeof and instanceof
var someVar:Number = 10;
trace(typeof someVar)
// returns number
and
import flash.geom.Rectangle;
var someObject:Rectangle = new Rectangle();
trace(typeof someObject)
// returns object
trace(someObject instanceof Rectangle)
// returns true
If he does mean returning the data type of a variable, it would be like [AS]return typeof variable
//orreturn instanceof variable[/AS]
So to answer so ?'s:
When I say function signature I mean knowing a function’s parameters, the typeof each parameter and the return type of the function. I need to get this information without actually calling the function.
I am familiar with both typeof and instance of.
Let me rephrase a few things. I need to know 1) how many parameters a function has and 2) the requested typeof each parameter in the function.
I need to do this without actually calling the function. I also need to do this dynamically.
So everytime you call a function, make an array with types of parametres where the length of that array is also number of parametres… if you want to use different number of parametres in one function, then its harder. I searched for some parameter array, but it seem that nothing like that exist in AS so you would have to use only one paramtre (an array) and call all functions like
function someFunc(args:Array):Void {
trace("total number of parametres: "+args.length);
for (i=0; i<args.length; i++) {
trace("parameter "+i+": "+args*);
}
// if you want to store these inforamation, then use
// the function Ill declare below
makeArray("someFunc", args);
}
someFunc(["par1", "par2", 3]);
trace(someFunc_array);
// 3 string string number
function makeArray(name:String, args:Array):Void {
this[name+"_array"] = new Array();
this[name+"_array"].push(args.length);
for (i=0; i<args.length; i++) {
this[name+"_array"].push(typeof args*);
}
}
Here is the whole purpose of my problem. I made an ArgumentChecker which you can read about here:
http://www.kirupa.com/forum/showthread.php?t=214908
Basically I want to take that one step further since that only applies to strings. Many people are going to read this and ask why I wouldn’t just do this or that. The simple answer is that I want to do this in a particular manner for the sake of learning.
So let me try an anology:
- You want to buy a funky new CD player that requires batteries. (the function to be inspected)
- You don’t know what type of batteries it requires (batteries = parameters)
- It does not say what type of batteries it requires and most people won’t know until they purchase the item and open it up (calling the function)
- you want to figure out what type of batteries it requires without buying and opening the item (looking at the parameters without calling the function)
So now you can see where this is going. I want to be able to:
- peer into a targeted function (w/o calling it),
- check the type casting of its required parameters,
- use my argument checker to see if incoming parameters destined for the target function are type casted correctly,
- correct them if needed based on the target functions signature,
- pass the newly corrected parameters to the target function.
Someone here at work suggested that I check Reflection in teh as2lib but I wasn’t able to understand the documentation. Man I really hope this is possible. It would save a ton of work correcting crap that was created overseas, that I need to debug.