开发者

Passing optional arguments (...rest) through to another method that takes optional arguments in AS3

I have a "format" method that works in a similar manner to the C# String.Format method, with the following signature:

In a class named StringTools:

/**
 * Formats a string, based on C# String.Format method.
 * @param raw A string with numbered tokens, such as "{0}, {1}"
 * @param rest Values that replace the numbered tokens in raw.
 */
public static function format(raw:String, ...rest:*):String;

StringTools.format("{0}, {1}", "Hello", "World") returns the string "Hello, World" as expected. Now, I'm trying to get my logging class to use this method, but I'm having trouble passing the optional variables through. The signature of the method in the logging class is:

public static function infof(raw:String, ...rest:*):String;

If I pass "rest" directly into StringTools.format(raw, rest), it's passed in as an array, and not as a series of parameters, so if I call it liks this: infof开发者_如何转开发("{0}, {1}", "Hello", "World"), I get the string "Hello,World, {1}", since it replaces the first token with the entire array of values.

I also tried constructing an arguments array, and calling the method like this:

var collectArgs:Array = [raw];

for (var i:Number = 0; i < rest.length; i++)
{
collectArgs.push(rest[i]);
}

var callFunction:Function = StringTools.format.call;

trace(callFunction.apply(null, collectArgs));

However, this traces "World,6". So, it looks like the parameters are shifted. So, I tried initializing collectArgs as [null, raw], and I get "Hello World,6. The number is {1}" again.

Am I doing something wrong? What is the correct way to pass optional parameters from one method that expects optional parameters to another method that expects optional parameters?

Thanks!


I think you are on the right lines using apply. This seems to do illustrate the behaviour you want:

static function f1(raw:String, ...rest:*):void
{
    trace("f1: "+raw+" "+rest);
    rest.unshift(raw);
    f2.apply(null, rest); 
}

static function f2(raw:String, ...rest:*):void
{
    trace("f2: "+raw+" "+rest);
}

function passSomeArguments():void
{
    f1("A",1,2,3);
}

EDIT: You need to pass 'null' as the 1st parameter to apply because the first parameter is what is considered to be 'this' when the function is called. Since the functions are static (and in any case have no dependency on 'this') you can pass null, but you must pass something.


You could also do something like this (of course this is not best implementation for the string formatting):

public static function format(raw:String, ...rest:*):String {
    if (rest[0] is Array && rest.length == 1) {
        rest = rest[0];
    }
    var r:RegExp = /(\{\d+\})/g;
    var matches:Array = raw.match(r);
    for (var i:Number = 0; i < rest.length; i++) {
        raw = raw.replace(matches[i], rest[i]);
    }
    return raw;
}

Then your infof function would just look like this:

public static function infof(raw:String, ...rest:*):void {
    var formatted = StringTools.format(raw, rest);
}

As mentioned in my comment, if you remove the call method from the end of you callFunction setter, then you do not need to supply null as the first argument. See http://livedocs.adobe.com/ to understand what the call method actually does, and what the first parameter is for.

As @stephen mentioned, it is a lot simpler to unshift your raw var onto the rest array, rather than building up a new one.


Actually, just found that it's my problem. It should work fine using the argument collection method described, as long as the first element in the arguments array is null. I'm not sure why null is necessary, but it works fine this way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜