views:

37

answers:

5

Hi! I'm trying to define interface in which one method (initPage) needs to have different arguments in every implementation. How can I do this? Using ...args for me is not good, because I'm loosing strong type checking.

public interface IPage {
    function initPage(...args):void;
    function showPage():void;
    function hidePage():void;
    function removePage():void;
}
A: 

You cannot have an interface that defines a method where each implementation uses different arguments.

The purpose of an interface is to define a common set of method signatures of all classes that implement that interface.

Having each class take different arguments defeats this purpose, so you cannot do it without using rest parameters (...).

Alan Geleynse
+1  A: 

Having different arguments for different implementations of an interface goes against the fundamental purpose of an interface. If you only had a reference to the interface, how would you know what arguments to pass?

Richard Szalay
A: 

Using ...args for me is not good, because I'm loosing strong type checking.

so you know the certain amount of arguments and even their types so the only inconvinience i can imagine is that you will often have to call this function with no args. i usually solve it like this: private function doSomething(e: Event = null):void {}

and there's another way if you're not sure about this function - just pass it one object as an argument and change it to some custom associated-array-class as soon as you'll be sure about all data fields

www0z0k
yeah, the idea of passing an Object like: initPage({text:myXML.@title, color:myXML.@color}) looks better than ...args. thanks!
Vitamon
A: 

You could use the class constructor to pass any arguments you need and leave the arguments out of the initPage method.

public interface IPage 
{
  function initPage():void;
  function showPage():void;
  function hidePage():void;
  function removePage():void;
}

//any class that implements IPage can have a different
//set of arguments
public class A implements IPage
{
    //for example...
    private var obj1:String;
    private var obj2:int;

    public function A( obj1:String , obj2:int )
    {
       this.obj1 = obj1;
       this.obj2 = obj2;
    }

    public function initPage():void
    {
        //obj1 & ob2 are available here
     }
}
PatrickS
I'm using MovieClip instances that are already on the stage, so their constructors are called before I can initialise them with specific data loaded from XML
Vitamon
A: 

If the different objects that the initPage accept as parameter share a certain structure, they could all extend another interface or parent class. For example having:

function initPage(arg:DisplayObject):void;//For example

The initPage could receive anything like Bitmap, Sprite, or MovieClip objects retaining at least some strict typing. The code within each implementation of the method could at least make sure that such implementation is receiving the expected object type.

LopSae
It still doesn't solve the problem of passing specific set of parameters to specific page. I think I could pass parameters in Object class like: initPage({text:myXML.@title, color:myXML.@color}), this would be more strict, 'cause I would have to check at least field names.
Vitamon