Abstract methods confusion
Monkey Forums/Monkey Programming/Abstract methods confusion
| ||
Hello all, I finally took the plunge and purchased a copy of Monkey. After having a play it seems like it could become a great tool with a little bit more love and attention in a few key areas. Great job so far though! Anyway, on with my question which I posted on the Blitz forums a few months back but it is still something which is bothering me so I thought I`d ask again here now that Monkey has evolved a little. In Blitz Max you can make a base type like TEntity and then extend your other types from this. If TEntity has an abstract method, like Method Draw() Abstract then any types that extend it MUST include their own Draw() method or else an error occurs. This makes sense to me and works as I expect it to but in Monkey I can set up a class called Entity, give it an abstract method, again Method Draw() Abstract, but now if I extend this class, Class Player Extends Entity and purposely leave out the Draw method it still works, shouldn`t this produce an error? If not then what use is Abstract in Monkey? Can someone please explain what I`m missing here? Thanks for any help, Jason. |
| ||
You're right. It is not behaving as in Blitz Max. This compiles without errors, and it shouldn't (well, according to Blitz Max rules anyway) Strict Class Temp Abstract Method Draw:Void() Abstract End Class Temp2 Extends Temp End Function Main:int() Local d:= new Temp2 End |
| ||
This is something to do with the fact that Monkey only tries to compile methods that are actually used, but look at this:Strict Class Temp Abstract Method Draw:Void() Abstract End Class Temp2 Extends Temp End Class Temp3 Extends Temp Method Draw:Void() End End Function Main:Int() Local d:= New Temp2 Local temps:= New List<Temp3> For Local t:=Eachin temps t.Draw Next End this doesn't compile, but there's no way an object of type Temp2 can have its Draw method called. If you change d to a Temp3 or comment out the t.Draw line it compiles. |
| ||
Agreed. This shouldn't compile EVER regardless of what parts of the classes you use. If you change New Temp2 to New Temp3, it should still fail unless you also declare Temp2 as Abstract. |
| ||
Posted in the bugs forums for an official response. Jason. |
| ||
Agreed. This shouldn't compile EVER regardless of what parts of the classes you use. That is a feature of Monkey. It will only compile/translate what you use. I am quite happy about it as it won't add functions to your code that you don't need. Keeps the resulting target files smaller. |
| ||
That is a feature of Monkey. Breaking inheritance rules is not a feature, it's a bug. Although it's great that Monkey only includes the things you use, it should still be lexing and validating the entire codebase first. |