Field Overrides in Types

BlitzMax Forums/BlitzMax Programming/Field Overrides in Types

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Heh, answered my own question (once I understood enough to ask the second, updated question above). Yes, it's scope-dependent - now everything is clear to me. The modified code below shows this for anyone else flummoxed by this as I was:
Strict


Type TGeneric Abstract

	Field MyType$ = "generic"

	Method SpecMyType() Abstract
	
	Method GenMyType()
		Print "GenMyType: "+MyType
	End Method

End Type


Type TSpecific Extends TGeneric

	Field MyType$ = "specific" 'COMMENT THIS LINE OUT TO CHANGE BEHAVIOR

	Function Create:TSpecific()
		Local c:TSpecific=New TSpecific
		c.MyType="specific"
		Return c
	End Function

	Method SpecMyType()
		Print "SpecMyType: "+MyType
	End Method

End Type


'Two functions to access MyType, one takes a TGeneric parameter while the other takes a TSpecific

Function PrintGeneric ( thing:TGeneric )
	Print "PrintGeneric: MyType="+thing.MyType
	thing.GenMyType
	thing.SpecMyType
End Function


Function PrintSpecific ( thing:TSpecific )
	Print "PrintSpecific: MyType="+thing.MyType
	thing.GenMyType
	thing.SpecMyType
End Function


'Three ways of creating a new instance of TSpecific:

Local Specific1:TSpecific = New TSpecific

Local Specific2:TSpecific = New TSpecific
Specific2.MyType = "specific"

Local Specific3:TSpecific = TSpecific.Create()


Print
Print "Specific1:"
PrintGeneric( Specific1 )
PrintSpecific( Specific1 )

Print
Print "Specific2:"
PrintGeneric( Specific2 )
PrintSpecific( Specific2 )

Print
Print "Specific3:"
PrintGeneric( Specific3 )
PrintSpecific( Specific3 )


What was tripping me up was that I wanted to be able to pass Specific instances as TGeneric items, so that the accepting function could handle additional variations of TGeneric as well. But I was having trouble accessing the overridden MyType when passing them that way (even though the overridden Methods worked as they should).

The reason is that directly accessing MyType when passed as TGeneric was reading the TGeneric-level MyType. This does make sense, but the TSpecific-level Methods were accessible even when passed that way, which confused me. I see now that Methods override, while Fields don't - unless they aren't defined in the Extending Type, in which case they "do" (they aren't really overriding, but instead referring to the higher-level, Abstract, Field).