why overloading not support in Actionscript?
Asked Answered
C

5

5

Action script is developed based on object oriented programming but why does it not support function overloading?

Does Flex support overloading?

If yes, please explain briefly with a real example.

Carrera answered 24/11, 2009 at 17:41 Comment(1)
Flex is "just" a framework.. so it offers nothing more then actionscript 3Stay
L
22

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

Lunsford answered 24/11, 2009 at 17:50 Comment(0)
D
9

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.

Dorsey answered 24/11, 2009 at 17:55 Comment(2)
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)Broyles
You're correct, what I'm saying is that within the function rest is an Array of the comma-separated parameters.Dorsey
P
1

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];
        }
    }

}
Poundal answered 24/11, 2009 at 18:13 Comment(2)
Maybe you can enlighten the OP as to why, lk?Seizure
I would say, it's JavaScript way:)Overfeed
L
1

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.

Lorca answered 24/11, 2009 at 21:5 Comment(0)
A
1

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.

Austenite answered 24/11, 2009 at 21:15 Comment(2)
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.Aden
Thanks for the update! Did you mean that AS2 looks up functions by name?Austenite

© 2022 - 2024 — McMap. All rights reserved.