AS3 Recommendations for a method that can return two types

I am developing a framework for ActionScript 3 and come across a specific scenario when I want a method to be able to return either AvFrameworkObjector Arraycontaining multiple instances AvFrameworkObject.

Take this method, which is used to register one or more instances AvFrameworkObject:

public function register(...objects):Object
{
    var reg:Array = [];

    var i:AvFrameworkObject;
    for each(i in objects)
    {
        i.fw = this;
        reg.push(i);
    }

    return reg.length > 1 ? reg : reg[0];
}

A few things above:

  • It seems to me very sloppy. It's really?
  • I can use a wildcard (asterisk *) as the return type, what makes sense?
  • Is it possible to actually define two return types only ? Just for readability, etc.
+3
source share
3

, :

ActionScript 3.0 , . , , Boolean, Number, int, uint String, , , .

, fw .


.

  • . var length:int = register(out, object1, object2, object3). length for.
  • - . , , , , . , , t:Thing = fw.register(new Thing())[0];. [0] .
  • , Object insted Array.

, .

, .

+3
  • - , .

  • . , * IMO, Object.

  • , .

, .

, , 1 , 1?

+4

1 . ; , , , , - .

Now a situation also arises when the code will respond in exactly the same way as the returned object, regardless of what type it is, in this case this means that the returned objects are inherited from some common base class. In this case, the return type must be a common base class.

(I suppose I assume that you will not respond exactly the same as in Array and AvFrameworkObject)

0
source

All Articles