Given an ActionScript Function object, is there any way to determine whether that function has one or more optional parameters, or vararg parameters? The length
property seems to return the minimum number of arguments accepted:
function vararg(a:*, b:*, ...rest):void {}
function optional(a:*, b:* = null, c:* = null):void {}
trace(vararg.length); // 2
trace(optional.length); // 1
I've tried reflecting over the function properties:
for (var name:String in optional) {
trace(name + ": " + optional[name];
}
However this did not output anything at all.
Does anyone know how to discover this information through reflection?
Well, I can get you a little bit closer, but not all the way.
If you call describeType
on the object that has the function AND those functions are public, you will get more information about the functions:
var description:XML = describeType(this);
var testFunction:* = description.method.(@name == "optional")[0];
trace(testFunction);
This will give you useful output:
<method name="optional" declaredBy="MyClass" returnType="void">
<parameter index="1" type="*" optional="false"/>
<parameter index="2" type="*" optional="true"/>
<parameter index="3" type="*" optional="true"/>
<metadata name="__go_to_definition_help">
<arg key="file" value="/path/to/MyClass.mxml"/>
<arg key="pos" value="222"/>
</metadata>
</method>
It also won't tell you about the ...rest
varargs. So, there are two caveats: they must be public AND you don't get varargs... but you do get a lot more information...
I am not sure you will be able to get any more information than this.
I've always thought describeType
needs to be able to reflect on private stuff as well... but alas.