David Wolever
David Wolever

Reputation: 154564

ActionScript: Determine wether superclass implements a particular interface?

Is there any non-hacky way to determine wether a class' superclass implements a particular interface?

For example, assume I've got:

class A extends EventDispatcher implements StuffHolder {
    protected function get myStuff():Stuff { ... };

    public function getStuff():Array {
        if (super is StuffHolder) // <<< this doesn't work
            return super['getStuff']().concat([myStuf]);
        return [myStuff];
}

class B extends A {
    override protected function get myStuff():Stuff { ... };
}

How could I perform that super is StuffHolder test in a way that, well, works? In this case, it always returns true.

Upvotes: 1

Views: 196

Answers (3)

David Wolever
David Wolever

Reputation: 154564

I could do something involving introspection, getting the current class, finding its parent class, then checking to see if that implements the StuffHolder interface… But that seems pretty ugly :(

Upvotes: 0

mrkishi
mrkishi

Reputation: 5952

I don't have the full picture to figure out why you'd need that kind of (weird and possibly broken) inheritance, but could you rephrase it to if (this is actually an A instance)?

If so, you could go with...

import flash.utils.getQualifiedClassName;

if (getQualifiedClassName(this) == 'A')

The thing is, the is operator should be used on object instances. And it works all the way up to object -- I mean, A is B is true for every A ancestor or interface implementation.

I think you could come up with a better structure for your classes, though. This isn't that pretty.

Upvotes: 1

Sandro
Sandro

Reputation: 4771

In this case you might have to define StuffHolder (and have it extend EventDispatcher) as a class and have getStuff as a public/protected function. You could then overload the getStuff function in class A, but not in class B.

package {
 public class StuffHolder extends EventDispatcher {
  function StuffHolder() {

  }
  protected function getStuff():Array{
   return ["Default"];
  }
 }
}
package {
 public class A extends StuffHolder {
  function A {
   super();
  }
  protected override function getStuff():Array {
   return ["A"];
  }
 }
}
package {
 public class B extends StuffHolder {
  function B {
   super();
  }
 }
}

Upvotes: 1

Related Questions