[HELP] Multiple inheritance or other solutions

I have a problem and I will just make it simpler to explain it.

I have two classes, let’s say a Shape and a Sprite.
I want to implement a method in both, let’s say this one

public function clearGraphics():Void {
      graphics.clear();
}

what is the best way to do it?

If I make two different subclasses (ShapeClear and SpriteClear) I have to implement the method twice, and I don’t like it. I want to write the method once and use it in both subclasses.

For now I wrote an interface, implemented it in both subclasses and wrote the same method in both. Not good.

Ideas?
Maybe an Abstract (I didn’t really understand what it is exactly) or multiple inheritance?

Thank you

PS: I know it could be easy to just make a new static class with a method like this

public static function clearGraphics(element:DisplayObject) {
    element.graphics.clear();
}

but if I have a lot of methods it becomes still pretty unpractical

For your example you wouldn’t need to write a method, you could just call element.graphics.clear(), but assuming you want more functionality. Think about the OOP concepts here. You haven’t given us a lot of detail about what ShapeClear and SpriteClear are, but if SpriteClear is a Shape Clear then you can write class ShapeClear extends SpriteClear and if SpriteClear is a Sprite you can write class SpriteClear extends Sprite

What if you used that as a static extension?

Sorry Andy maybe I was not clear…

I’m actually talking about openfl.display.Sprite and openfl.display.Shape
I can’t extend, they already exists.

But I want to add a method on both, or actually add a method on both my extended classes

class SpriteClear extends openfl.display.Sprite

and

class ShapeClear extends openfl.display.Shape

and my objective it is writing this only once, so when I want to change it I have to do it only in one place.

I’m trying to think OOP, I don’t know the answer.

This is very interesting but I can’t see a way to implement it in my case.
I don’t want to add ONE/MULTIPLE methods to ONE class, I want to add ONE/MULTIPLE methods to MULTIPLE classes writing once.

Write once, use lots. Why? Because if you write the same more than once you will forget and when you change the code some copies will not be changed and it is a mess.

I think static extension are meant to add functionality to ONE THING you can’t change and you don’t want to extend.


Maybe what I want is just a #include in the ANSIC way, a MACRO before the compilation, just to copy that code in my classes from a single source.


Maybe my objective is wrong in the idea itself, I’m asking to understand.

Got it working. The trick is that the typedef’s graphics field needs the correct access modifiers (default and null in this case).

Press the “Build + Run” button to see it in action.

that is pretty sick mate. brilliant.
I’ll try to implement it the way I need tomorrow thanks a lot for the good advice and proof of concept!!!

Wow, really nice solution! I had not heard of static extensions before!

I would indeed have implemented it as a util class with static functions. Do you know if the static extension approach performs better? It would have an impact if the clearGraphics is called each frame.

It actually performs the same. It’s a compile-time feature only.

//You write:
sprite.clearGraphics();

//Haxe generates:
GraphicsExtension.clearGraphics(sprite);