0

I am trying to create an ActionScript 3 class that implements two interfaces. The interfaces contain member functions with different signatures but the same name:

public interface IFoo
{
    function doStuff(input:int):void;
}

public interface IBar
{
    function doStuff(input1:String, input2:Number):void;
}

public class FooBar implements IFoo, IBar
{
    // ???
}

In C# (for example) this is not a problem because methods can be overloaded, but ActionScript does not support overloading. Is there any way to create a class that implements both interfaces?

Aaron
  • 2,013
  • 16
  • 22

2 Answers2

2

No, unfortunately this is not possible and it's because of the reason you already pointed out: ActionScript 3 does not support member overloading. It's a shame, but it's the unfortunate truth.

It is possible to have multiple members with the same name and even the same signature in a class, however, they must be qualified by namespace in that case. For instance, this should work:

public namespace foo;
public namespace bar;

foo function doStuf(input:int):void
{
    // ...
}

bar function doStuff(input1:String, input2:String):void
{
    // ...
}

You then reference the methods by qualifying them like so:

foo::doStuff(1);
bar::doStuff("foo", "bar");

Unfortunately, this won't help with your problem because even though the namespaces may be in the public namespace, they are still not the same as the public namespace itself meaning you're not satisfying the contract set forth by the interfaces (everything must be public). Making a long story short; unless you use some sort of composite pattern, you're out of luck until Adobe decides to implement member overloading.

Marcus Stade
  • 4,724
  • 3
  • 33
  • 54
  • I was afraid that was the case, but thanks for the clear answer. Could you expound on what you mean by using a "composite pattern?" Do you mean creating two classes, one for each interface, and then allowing them to communicate or share state somehow? – Aaron Dec 22 '10 at 01:12
  • Yeah, that's it. In order to promote loose coupling, I'd suggest that you let the aggregating class handle the communication. – Marcus Stade Dec 22 '10 at 04:51
  • I'm afraid my last comment might have been unclear there. What I mean is that if you have two separate classes, one for each interface, you can wrap them in an aggregate class that stores references to instances of these classes and facilitates any communication in between. Any implementation details could be shared if necessary by using the internal namespace (if they are in the same package) or by using custom namespaces. This would be somewhat like a friend class. It's certainly not ideal, but it's something at least. – Marcus Stade Dec 23 '10 at 00:57
  • Answer accepted. I've long forgotten what the original issue in my project was (clearly I found some way to work around it), but this seems like about as good a solution to this problem as I could hope to find. – Aaron Feb 05 '11 at 04:51
1

public class FooBar would have to implement both interaces and thus implment those functions listed. Problem is ActionScript does not support method overloading. It is a nice feature that I miss from C# :(

Allan
  • 3,339
  • 2
  • 21
  • 24
  • You're right; explicit interface implementations aren't even necessary in C# to solve this problem; simple method overloading will do. I updated the question to reflect this. However, the question was "Is there any way to create a class that implements both interfaces?" so your answer isn't really an answer. – Aaron Apr 21 '10 at 14:54