Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3: two functions with the same name but incompatible signatures from two interfaces

I have two interfaces that declare functions with the same name but incompatible signatures:

interface IDog 
{
    function bark() : void;
}

interface IAdvancedDog
{
    function bark(volume : Number) : void;
}

I need to implement both in the same class, something like the following:

class AlphaDog implements IDog, IAdvancedDog
{
     public function bark() : void
     {
     }

     public function bark(volume : Number) : void
     {
     }
}

This, of course, doesn't work. Is there a way to disambiguate bark() functions in this case? C#, for example, allows an explicit interface marker:

   class AlphaDog : IDog, IAdvancedDog
   {
       // ...
       void IDog.bark() { } 
   }

Although this won't be required in C# due to function overloading rules. There's no overloading in AS3, so is there any other way to resolve this?

UPDATE.

1) Missing 'public' qualifiers in the class definition were a typo. I fixed it.

2) I missed an additional requirement that interfaces CAN'T be modified (for the purpose of this question). In the real project they're defined in two different libraries that are a part of a large project. Multiple classes implement both interfaces (separately). So, any changes of interfaces will require cascading updates of all those classes, recompilation, testing, etc. So, I wanted first to find out if there was a solution without such intrusive modifications. It doesn't seem there is.

like image 992
Alex Avatar asked Dec 16 '25 15:12

Alex


1 Answers

ActionScript 3 does not support function overloading like this. Plus the fact that you did not define if the functions as public you will get compile errors.

What you can do in your case is add a default of NaN and only have one interface.

package{
  public class AlphaDog implements IDog
    {
         public function bark(volume : Number = NaN) : void
         {
         }
    }
}


package{
    interface IDog
    {
        function bark(volume : Number = NaN) : void;
    }
}
like image 130
The_asMan Avatar answered Dec 19 '25 06:12

The_asMan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!