1

I am new to ActionScripting but I have done some Java. I was told they are kinda similar. I am coding my swf file with some AS3 integrated.

function init():void{
  // do something
}

function init(var string:String):String{
  // do something else
}

is this not allowed in AS? If not, is there another way of handling it besides?

Thanks in advance.

seroth
  • 581
  • 1
  • 9
  • 26
  • Just to add, I ran this and got the error 1021: Duplicate function definition. Sorry, should have added that in the initial. – seroth Jul 02 '13 at 16:22

3 Answers3

5

Yes, you can override functions. But the example you gave is not overriding - it's overloading. For overriding a function, you basically just create a function with the same signature and everything in a subclass and add the word "override" right before it.

You can't directly overload a function though. If you want a variable number of parameters, you have to use optional parameters instead. Like this:

function init(str:String = null):String
{
    if (str == null)
    {
        // do one thing
        return null;
    }
    else
    {
        // do another thing
        return "someString";
    }
}

And that's about the best you're going to be able to do in AS3. The inability to overload functions, at least strictly speaking, is a fairly common complaint and obvious shortcoming of the language.

Panzercrisis
  • 4,590
  • 6
  • 46
  • 85
1

Do you mean method overloading? Actionscript, sadly, does not support this.

To get around it, you can use default parameters, or just make your parameters a bit less constraining. This answer has some details on that.

You could try this:

function init(var string:String = "Default value"):String{
  // do something 
}
Community
  • 1
  • 1
austin
  • 5,816
  • 2
  • 32
  • 40
  • overloading, Yes :-(. Does it handle method overloading a different way or not at all. – seroth Jul 02 '13 at 16:25
  • @seroth Not at all. There may only be a single method with a given name. This is most likely because AS has high-level function types. – poke Jul 02 '13 at 16:27
1

Actionscript does not support method overloading. However, based on the answer to this question you have other options.

If you just want to be able to accept any type, you can use * to allow any type:

function someFunction( xx:*, yy:*, flag:Boolean = true )
{
  if (xx is Number) {
    ...do stuff...
  } else if (xx is String) {
    ...do stuff...
  } else {
    ...do stuff...
  }
}

If you have a large number of various parameters where order is unimportant, use an options object:

function someFunction( options:Object )
{
  if (options.foo) doFoo();
  if (options.bar) doBar();
  baz = options.baz || 15;
  ...etc...
}

If you have a variable number of parameters, you can use the ... (rest) parameter:

function someFunction( ... args)
{
  switch (args.length)
  {
    case 2:
      arr = args[0];
      someBool = args[1];
      xx = arr[0];
      yy = arr[1];
      break;
    case 3:
      xx = args[0];
      yy = args[1];
      someBool = args[2];
      break;
    default:
      throw ...whatever...
  }
  ...do more stuff...
}

For cases where you need to call a common function to a number of classes, you should specify the interface common to each class:

function foo( bar:IBazable, flag:Boolean )
{
  ...do stuff...
  baz = bar.baz()
  ...do more stuff...
}
Community
  • 1
  • 1
Jason Towne
  • 8,014
  • 5
  • 54
  • 69