Overloading with different parameters
Monkey Forums/Monkey Programming/Overloading with different parameters
| ||
Hi, I was searching the forums but I am not sure if I found a "suitable" explanation (or at least one that I could understand). I thought one was able to overload methods with different parameters like this: Class c_baseClass Method change() Abstact End Class c_classA Extends c_baseClass Method change(param:int) End Class c_classB Extends c_baseClass Method change(param:int, param2:int) End I am getting an error here...Any ideas? |
| ||
Your Methods do not have an End StatementClass c_baseClass Method change() Abstact End End Class c_classA Extends c_baseClass Method change(param:int) End End Class c_classB Extends c_baseClass Method change(param:int, param2:int) End End |
| ||
Hi Trez, Totally true but that was just an example. Even with the End statements I don't believe that this kind of overloading is allowed. |
| ||
Extending classes are not allowed to generate new overloads in Monkey. |
| ||
This would work:Class c_baseClass Abstract Method change(); End Method change(param:Int); End Method change(param:Int, param2:Int); End End Class c_classA Extends c_baseClass Method change(param:Int) End Method End Class c_classB Extends c_baseClass Method change(param:Int, param2:Int) End Method End @Trez Class c_baseClass Method change() Abstact End there is nothing wrong with that code and is the correct way of using it. |
| ||
Ah! Thanks Jesse! That will totally solve my problem. Great ideas and help as usually around here. |
| ||
Take into account that, in Monkey, if you override a method with several overloads, only the overriden ones are available to be called later. Exampe: Class A Method DoSomething(value1:Int) end Method DoSomething(value1:Int, value2:Int) end End Class B extends A Method DoSomething(value1:Int) End End Function Main() Local a:=New A a.DoSomething(10) a.DoSomething(10,20) Local b:=New B b.DoSomething(10) b.DoSomething(10,20) '<--- This fails End |
| ||
but this work:Class A Method DoSomething(value1:Int) end Method DoSomething(value1:Int, value2:Int) end End Class B extends A Method DoSomething(value1:Int) End End Function Main() Local a:=New A a.DoSomething(10) a.DoSomething(10,20) Local b:A=New B b.DoSomething(10) b.DoSomething(10,20) '<--- This don't fail End |
| ||
Take into account that, in Monkey, if you override a method with several overloads, only the overriden ones are available to be called later. Exampe: Class A Method DoSomething(value1:Int) end Method DoSomething(value1:Int, value2:Int) end End Class B extends A Method DoSomething(value1:Int) End End Function Main() Local a:=New A a.DoSomething(10) a.DoSomething(10,20) Local b:=New B b.DoSomething(10) b.DoSomething(10,20) '<--- This fails End Looks like a bug. Class B inherits/extends Class A, so Class B contains everything from Class A. The error message says "--> Unable to find overload for DoSomething(Int,Int)." If it can't find the correct overload in Class B, it should check in the parent Class A, and there it is. This works correctly (it finds the correct overloads): Class A Method DoSomething(value1:Int) End Method DoSomething(value1:Int, value2:Int) End End Class B extends A End Function Main() Local a:=New A a.DoSomething(10) a.DoSomething(10,20) Local b:=New B b.DoSomething(10) b.DoSomething(10,20) End Overriding both methods works, too: Class A Method DoSomething(value1:Int) End Method DoSomething(value1:Int, value2:Int) End End Class B extends A Method DoSomething(value1:Int) End Method DoSomething(value1:Int, value2:Int) End End Function Main() Local a:=New A a.DoSomething(10) a.DoSomething(10,20) Local b:=New B b.DoSomething(10) b.DoSomething(10,20) End Doesn't make sense that it fails when out-commenting one method in Class B. |
| ||
Regarding your to 2nd code snippet. Are you sure that this is actually a problem? In code snippet #2 you do not actually do any overloading in Class B. You are only extending Class A which means that Class B will inherit the methods from Class A. So I believe that there is no overloading here, only inheritance. So I believe it works as intended. |
| ||
Looks like a bug. Class B inherits/extends Class A, so Class B contains everything from Class A. Yes, it looks like a bug but it's how it's actually implemented. When Trans solves overloading on a method, it iterates through the inheritance chain and, in the first level it finds a method declaration, it gets all possible overloads (without further iterating the inheritance chain). Not a big issue if you know it, but not as nice as it could possibly be. It got me some Jungle iterations to get this right on the intellisense! |