Code Design Question
Monkey Forums/Monkey Programming/Code Design Question
| ||
Monkey doesn't seem to support differing return values for overridden methods.. e.g.Class Base Field name : String Method Copy : Base() Return Base.Create( Self.name ) End Method Function Create : Base( n : String ) Local this : Base = New Base() this.name = n Return this End Function End Class Class Bar Extends Base Field value : Float Method Copy : Bar() Return Base.Create( Self.name, Self.value ) End Method Function Create : Bar( n : String, v : Float ) Local this : Bar = New Bar() this.name = n this.value = v Return this End Function End Class Function Main : Int() Local b : Bar = Bar.Create( "hi", 0.5 ) b = b.Copy() Return 1 End Function This means that I would need to cast b = Bar( b.Copy() ) and change the return values of all overriding copy methods to return Base. Is there a clean elegant way around this? Perhaps I'm being too fussy... |
| ||
I don't believe any major languages allow this. It makes polymorphism rather difficult if functions can return different types in extended classes, although I can see that there could be some reason for allowing them to return their own type as above. You could use a set of functions I guess: Function Copy:Base( Base ) Function Copy:Bar( Bar ) |
| ||
Yea functions are an option.. might stick with what I have for now, can always fall back to functions as they will be really simple to implement. I can't figure out a cleaner / cleverer way. |
| ||
Maxgui on Max does the same, always returns a TGadget and the TGadget class has all methods of its extending classes as a "1-liner" Method getText$() End Method So you can use your Bar methods with a Base instance |