Interfaces for BlitzMax

BlitzMax Forums/BlitzMax Programming/Interfaces for BlitzMax

Got an idea from the other thread about implementing Java-like interfaces using reflection and meta data.

Does this look in any way useful?

SuperStrict

Framework BRL.StandardIO

Import BRL.LinkedList

Import "Interface.bmx"

Type TBaseType
	
	'This would contain something that TSubType needs, but TAnotherType doesn't
	
End Type

Type TSubType Extends TBaseType { Implements ICallable }
	
	Method Call(message:Object)
		Print message.ToString()
	End Method
	
End Type

Type TAnotherType { Implements ICallable }
	
	Method Call(message:Object)
		Print "Got message: "+message.ToString()
	End Method
	
End Type

Type ICallable Extends TInterface
	
	Method Call(message:Object)
		_method "Call", [message]
	End Method
	
	Function Cast:ICallable(o:Object)
		Return ICallable(TInterface._cast(o, "ICallable"))
	End Function
	
End Type

'Sets up my interface implementation
TInterface.Setup()

'Create a couple of objects
Local s:TSubType = New TSubType
Local a:TAnotherType = New TAnotherType

'Add to a list
Local list:TList = New TList
list.AddLast s
list.AddLast a

'Enumerate the list as ICallables
For Local i:ICallable = EachIn ICallable.Cast( list )
	i.Call "Hello World!"
Next

'Cast the objects directly to ICallables
Local i:ICallable
i = ICallable.Cast(s)
i.Call "Hi!"
i = ICallable.Cast(a)
i.Call "Hi!"

Output:
Hello World!
Got message: Hello World!
Hi!
Got message: Hi!