Method Overloading - Kinda :)

BlitzMax Forums/BlitzMax Programming/Method Overloading - Kinda :)

gman(Posted 2006) [#1]
well who says you cant overload a method in BMAX. maybe this is an undocumented feature, maybe not. sorry if this is already known info. i did some brief searching and didnt find anything like it in the forums.

i stumbled on this technique today while working on upgrading the Irrlicht mod. there are some definate limitations to this, but i do see some potential.
' LIMITATIONS:
' must have abstract layer type
' cant call Super instance of same method directly
' can only have one overload per method

Framework BRL.Basic

Type base
	Method A:Int(param1:Float,param2:Int)
		Print param1
		Return param2
	EndMethod

	Method B:String()
		Return "base B"
	EndMethod
EndType

' abstract layer to facilitate overload
Type base_overload Extends base
	' abstract overload of method A
	' NOTE: completely different params both count and type and also return type is different
	Method A:String(param1:String,param2:Float,param3:Int) Abstract
	
	' wrapper of the super A so we can still call it
	' NOTE: other super methods that are not overridden with abstract will not need a wrapper
	' and can be called in extended types with regular "super" syntax
	Method Super_A:Int(param1:Float,param2:Int)
		Return Super.A(param1,param2)
	EndMethod
EndType

Type MyType Extends base_overload
	' implementation of the abstract overload
	Method A:String(param1:String,param2:Float,param3:Int)

		' call to non-overloaded super method
		Print Super.B()

		' call the original A()
		param2:+Super_A(param2,param3)
		Return "RETURN:"+param1+" | "+param2+" | "+param3
	EndMethod
EndType

Local test_base:base=New base
Local test_overload:MyType=New MyType

Print "RETURN:"+test_base.A(11,8)
Print test_overload.A("now for something completely different",1,2.1)

have fun :)


H&K(Posted 2006) [#2]
well...

Some indication by BRL, that this "Undocumented feature" is going to stay, and youve found a very useful tool.

Mindyou true overloading would be good. Oh UM.


Mark Tiffany(Posted 2006) [#3]
That seems like a bug, not an undocumented feature to me - I wouldn't rely on it.