Missing Method Overloading in AS3
While I do like to write UI code in ActionScript3 within the flex framework, I also miss features from Java sometimes. One of those missed features is method overloading, the ability to have several methods with the same name, differing only in input and output types. This is not possible in as3.
Just Use Default Values Then
Well, this will only solve the special cases where the number of parameters differ, and not solve it really well either. In this example, where a ‘Person’ constructor takes several arguments, the default values approach will suffice:
public class Person {
private var name : String;
private var age : Number;
public function Person(name : String, age : Number = Number.NaN) {
this.name = name;
this.age = age;
}
}
That same code, I would have written like this in Java:
public class Person {
private String name;
private Integer age;
public Person(String name) {
this(name, null);
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
}
But, let us take another, real world example. Given a class ‘Task’, which can be constructed with a ‘title’ and a bunch of ‘tags’. The tags can be given either as an ‘ArrayCollection’ (of Strings) or as a comma-separated ‘String’ type, which will then be converted to ‘ArrayCollection’ in the constructor. In as3, I end up with this code:
public class Task {
private var title : String;
private var tags : ArrayCollection = new ArrayCollection();
public function Task(title : String, tags : Object) {
this.title = title;
if (tags is String) {
this.tags = convertTagStringToArrayCollection(tags as String);
} else if (tags is ArrayCollection) {
this.tags = tags as ArrayCollection;
} else {
throw new ArgumentError("value must be either String or ArrayCollection");
}
}
}
and this sucks. Switching out on the type and throwing ‘ArgumentError’ on non-allowed types. The compiler cannot check the types and I will have to apidoc which types are allowed on the ‘Object’ typed second paramter. In Java, I would have done this:
public class Task {
private String title;
private List<String> tags = new ArrayList<String>();
public Task(String title, String tags) {
this(title, convertTagStringToArrayCollection(tags));
}
public Task(String title, List<String> tags) {
this.title = title;
this.tags.addAll(tags);
}
}
which is much cleaner, and can be type-checked at compile time.
These examples are with constructors, but as3 does not support method overloading at all, so the problem exists for all functions and for property methods like ’set’ and ‘get’ too.


There is another way, but it’s not too clean either:
July 8th, 2008 at 22:41http://flexblog.faratasystems.com/?p=100
Not sure if it works for constructors, but perhaps defining different namespaces for functions with the same name will get you what you want.
July 8th, 2008 at 22:57I agree completely. Function overloading, generics and true abstract classes are the main things that I really miss coming from Java and C#.
As a guy who’s been using flash since before actionscript existed, I am very appreciative of what has been done to advance it, but these would make it complete for me.
July 9th, 2008 at 15:19