views:

114

answers:

2

I have a static Settings class where my application can retrieve settings from. The problem is that some of these settings are strings, while others are ints or numbers. Example:

package
{
    public final class Settings
    {
     public static function retrieve(msg:String)
     {
      switch (msg)
      {
       case "register_link":
        return "http://test.com/client/register.php";
        break;
                            case "time_limit":
                                   return 50;
                                   break;
      }
     }
    }
}

Now, in the first case it should send a string and in the second a uint. However, how do I set this in the function declarement? Instead of eg. function retrieve(msg:String):String or ...:uint? If I don't set any data type, I get a warning.

+2  A: 

Use *

public static function retrieve(msg:String):*
{
  if (msg == "age") {
    return 23;
  } else {
    return "hi!";
  }
}
HanClinto
+3  A: 

HanClinto has answered your question, but I would like to also just make a note of another possible solution that keeps the return types, typed. I also find it to be a cleaner solution.

Rather than a static retrieve function, you could just use static consts, such as:

package
{
    public final class Settings
    {
        public static const REGISTER_LINK:String = "my link";
        public static const TIME_LIMIT:uint= 50;            
    }
}

And so forth. It's personal preference, but I thought I would throw it out there.

Tyler Egeto
Thank you very much.
Tom
I agree. Although it is a useful thing to know how to return different types from a function ... I have *never* done it on a project. Using a class with static consts is not only cleaner, it will be much more performant.
James Fassett