Having a Method operate on many different TYPES.

BlitzMax Forums/BlitzMax Programming/Having a Method operate on many different TYPES.

Damien Sturdy(Posted 2006) [#1]
Hi All,

The code below fails.

I have a main type that needs to be able to operate on many different other types that share a common Pointer field. The problem is, using the data type "OBJECT" means Max can't find the pointer field of the passed object.

I *need* this to work- Why does it not, and how can it be made to?

Rem
I need the Method inside of type Test to be able to operate on any number of different other types.
The accepted types all contain a Pointer field.
Why does max spew up when I'm trying to do this?

End Rem

Type Test
	Field Pointer:Int
	
	Method Something(Thing:Object)
		DebugLog Thing.Pointer
	End Method
End Type

Type TestObject
	Field Pointer:int
End Type

Global OBj:TestObject=New TestObject
OBj.pointer=1024

Global Thistest:test=New test
DebugLog Thistest.Something(OBJ)



BlackSp1der(Posted 2006) [#2]
type extends?

Type Test Extends DebugObject
'	Field Pointer:Int
	
	Method Something(Thing:DebugObject)
		Return Thing.Pointer
	EndMethod
EndType

'----------------
Type A_Object Extends DebugObject
EndType
Type B_Object Extends DebugObject
EndType


	Type DebugObject
		Field Pointer:Int
	EndType
'-----------------

Global OBJ_A:A_Object=New A_Object
OBJ_A.pointer=1024
Global OBJ_B:B_Object=New B_Object
OBJ_B.pointer=512



Global Thistest:test=New test
DebugLog "Test type checking for pointer in OBJ of type A ="+Thistest.Something(OBJ_A)
DebugLog "Test type checking for pointer in OBJ of type B ="+Thistest.Something(OBJ_B)
DebugLog "Test type checking for pointer ="+Thistest.Something(Thistest)



Damien Sturdy(Posted 2006) [#3]
yeah i've considered that, but I see no reason why my original code can't work. Extending for the matter of a single field feels messy to me.


[Edit]

Ok, i've thought it through and I can see extending it is prob the cleanest way of doing it without having to cast a crapload of objects...


Thanks for your input.


Gabriel(Posted 2006) [#4]
Why does it not, and how can it be made to?

Because the Object type does not have a field called Pointer.

how can it be made to?

By using your own "object" type which extends the actual object type and adding a pointer field. But every type you pass to it will have to extend your new object type.

Type MyObject Extends Object
   Field Pointer:Int
End Type



Damien Sturdy(Posted 2006) [#5]
Oh well, I guess for some reason I overlooked the proper way to do it. Thanks goes to BlackSpider and Gabriel.