haXe Forum > Inheriting function with Interface?
-
Drakim Jul 18 at 09:50
I thought about several ways on how to explain exactly what I want, and decided I'll just say it as simple as possible:
I want an interface that has a fully defined function that is inherited by all the classes that implements the interface. It's important that this function is part of the interface, and not merely something extended from another class.
How would I go about doing this?
-
Andy Li Jul 18 at 11:42
You can define a base class(or abstract class) and let other classes extend it. If you do not want particular method be overrided, you may mark the method as inline.
-
Drakim Jul 18 at 12:51
I've been able to inherit methods though extending, but how do I make this melt perfectly into a interface? The problem I keep running into is that even though I end up with objects that have inherited this method, the method isn't part of the interface and thus cannot be used if I'm using this interface as the type for a collection.
-
Andy Li Jul 18 at 18:06
I'm a bit confused... Can you give some simple codes that illustrate the problem?
-
Drakim Jul 20 at 15:31
Sure!
Random example here with no connection to reality:
inteface Entity { var x:Int; var y:Int; function update():Void; function draw():Void; function getpositionsummed():Int { return x+y; } }
That's what I ultimately want, but it obviously doesn't work because you can't use interfaces that way. So, I'm guessing you need to use inheritance in some clever way combined with implementing. But when I tried, it refused to work out the way I wanted it. :/
-
hotpotato Jul 20 at 16:05
what you want is an abstract class, which is not possible in haxe. You can work around this issue with runtime errors, for example:
class AbstractA { // just implement first public function first () { return 1; } public function second () { throw "this method is abstract and must be overriden" } } class ConcreteA extends AbstractA { public function second () { return 2; } }
-
Drakim Jul 20 at 23:03
The idea did cross my mind, but it just seemed so....wrong.
I mean, if we are taking this route, isn't interfaces entirely superfluous, since you can replicate their function by just having an extended class you replace everything for?
I'll use it if that's really the best solution in HaXe, but I've noticed that it does prevent some things like using inline on the methods.
-
John Plsek Jul 21 at 11:04
How about
interface IInterface { public function first() : Int; public function second() : Int; } class AbstractA { // just implement first public function first () { return 1; } } class ConcreteA extends AbstractA, implements IInterface { public function second () { return 2; } }
-
Drakim Jul 22 at 02:33
Huh, I was sure I had tried that and that it didn't work. I guess I must have done something wrong back then. :s
I'll try it now.
-
John Plsek Jul 22 at 03:59
Try?
I wouldn't post code that doesn't work ;)
-
Drakim Jul 24 at 19:56
Yup, it works. I have no idea what I did different before, as this looks pretty identical to the very first thing I tried. ><
Now I got a different monster of a problem though, but I'll make another thread for that.
Thanks for the help!