views:

864

answers:

5

Hi,

Action script is developed based on Object oriented programming But why it is not support function overloading?

Flex is support overloading?

If yes, please explain briefly with real time example

Thanks, Ravi

+4  A: 

As you say, function overloading is not supported in Action Script (and therefore not even in Flex).

But the functions may have default parameters like here:

public function DoSomething(a:String='', b:SomeObject=null, c:Number=0):void

DoSomething can be called in 4 different ways:

DoSomething()
DoSomething('aString')
DoSomething('aString', anObject)
DoSomething('aString', anObject, 123)

This behavior maybe is because Action Script follows the ECMA Script standard. A function is indeed one property of the object, so, like you CAN'T have two properties with the same name, you CAN'T have two functions with the same name. (This is just a hypothesis)

Here is the Standard ECMA-262 (ECMAScript Language Specification) in section 13 (page 83 of the PDF file) says that when you declare a function like

function Identifier(arg0, arg1) {
    // body
}

Create a property of the current variable object with name Identifier and value equals to a Function object created like this:

new Function(arg0, arg1, body)

So, that's why you can't overload a function, because you can't have more than one property of the current variable object with the same name

PS: Sorry for my poor english

unkiwii
+5  A: 

It's worth noting that function overloading is not an OOP idiom, it's a language convention. OOP languages often have overloading support, but it's not necessary.

As lk notes, you can approximate it with the structure he shows. Alternately, you can do this:

public function overloaded(mandatory1: Type, mandatory2: Type, ...rest): *;

This function will require the first two arguments and then pass the rest in as an array, which you can then handle as needed. This is probably the more flexible approach.

Chris R
The ...rest parameter may not only be an Array, indeed it can be Zero to N parameters separated whit a comma. (PS: Sorry for my poor English)
unkiwii
You're correct, what I'm saying is that within the function `rest` is an `Array` of the comma-separated parameters.
Chris R
A: 

There is another way - function with any parameters returns anything.

public function doSomething(...args):*{
    if(args.length==1){
        if(args[0] is String){
            return args[0] as String;
        }
        if(args[0] is Number){
            return args[0] as Number;
        }
    }
    if(args.length==2){
        if(args[0] is Number && args[1] is Number){
            return args[0]+args[1];
        }
    }

}
ILya
This is so Anti-OOP :(
unkiwii
Maybe you can enlighten the OP as to why, lk?
Typeoneerror
it'a a good solution, if you really want to do this
Omu
A: 

You can't overload, but you can set default values for arguments which is practically the same thing, but it does force you to plan your methods ahead sometimes.

The reason it doesn't is probably mostly a time/return on investment issue for Adobe in designing and writing the language.

Jasconius
You're assessment of it being plain wrong is about as valid and factually based as my assessment that it is right. Unless you work at Adobe, which I doubt. I didn't present my comment as fact, merely as speculation, nor was it the bulk of my answer. So politely fuck off.
Jasconius
+1  A: 

Likely because Actionscript looks up functions by function name at runtime, rather than storing them by name and parameters at compile time.

This feature makes it easy to add and remove functions from dynamic objects, and the ability to get and call functions by name using object['functionName'](), but I imagine that it makes implementing overloading very difficult without making a mess of those features.

Dan Monego
While this is true for AS3, it's completely untrue for AS3 (to which this question refers). AS3 objects are static and have a method table. Expando objects are only available by marking a class as 'dynamic'. Late bound member access, such as your example, are handled explicitly by the AVM but it is not the default.
Richard Szalay
Thanks for the update! Did you mean that AS2 looks up functions by name?
Dan Monego