Inherited methods not used to complete Interface
Monkey Forums/Monkey Programming/Inherited methods not used to complete Interface
| ||
Looks like inherited methods are not used to determine if a interface has been implemented. The following code generates a error when it should not as the Interface has been implemented in the parent class.Strict Interface inter_i Method draw:Void(n:Int) End Interface Class lvl1_c Method draw:Void(n:Int) Print "levl1_c printing - "+n End Method End Class Class lvl2_c Extends lvl1_c Implements inter_i 'draw is implemented in lvl1_c ' Method draw:Void(n:Int)' ' Print "levl2_c printing - "+n ' End Method End Class Function Main:Int() Local a:inter_i = New lvl2_c a.draw(34) Return 0 End Function Of note, if you set the parent class as implementing the Interface. The Child classes inherit this interface as well. |
| ||
I think that's the way its supposed to work. Isn't it the point of interfaces that you have to implement them in every class that honours them? |
| ||
It is implemented by inheritance. |
| ||
This is a limitation of the way interfaces work in some target languages (C#/Java I think) so is therefore a limitation of Monkey. Basically, interface methods must be implemented in the 'implements' class (or, strictly speaking, a subclass). I suspect it's an efficiency thing, so that interface methods are all group into consecutive slots in a 'vtable' somewhere/somehow... |
| ||
public interface Inter { public void blah( ); } public class A { public void blah( ) {} } public class B extends A implements Inter { } Works fine in Java. |
| ||
Just for the record (if anyone comes across this in the future). You can sort of get around this by making some dummy functions/methods that call the super version of self. So the code above would look like... Class lvl2_c Extends lvl1_c Implements inter_i 'draw is implemented in lvl1_c Method draw:Void(n:Int) Super.draw(n) End Method End Class As I think of this, is this not something the compiler could handle for us all auto-magic like? |