Field Overrides in Types

BlitzMax Forums/BlitzMax Programming/Field Overrides in Types

WendellM(Posted 2005) [#1]
I'm trying to come to grips with how Extensions of Abstract Types handle Fields. The Methods of Extended Types replace the Abstract Methods that they're Extending - that's clear and works fine. However, the same doesn't seem to hold true for Types - at least not all the time.

That is, if I explicitly assign a default value to a Field in an Extending Type, it does not override the default value in the Abstract Field. But, if I don't put a Field entry at all in the Extending Type, then it does override the Abstract Field (when assigned by other means).

An example:


The code above produces this output:
Specific1:
PrintGeneric: MyType=generic
I'm specific (specific)
PrintSpecific: MyType=specific
I'm specific (specific)

Specific2:
PrintGeneric: MyType=generic
I'm specific (specific)
PrintSpecific: MyType=specific
I'm specific (specific)

Specific3:
PrintGeneric: MyType=generic
I'm specific (specific)
PrintSpecific: MyType=specific
I'm specific (specific)

So, there's no way to directly access MyType with a result of "specific" when passing it as TGeneric, though its GetType Methods will always return "specific."

However, if you comment out the indicated line in TSpecific - the one that assigns a default value of "specific" to MyType - then the results are different:
Specific1:
PrintGeneric: MyType=generic
I'm specific (generic)
PrintSpecific: MyType=generic
I'm specific (generic)

Specific2:
PrintGeneric: MyType=specific
I'm specific (specific)
PrintSpecific: MyType=specific
I'm specific (specific)

Specific3:
PrintGeneric: MyType=specific
I'm specific (specific)
PrintSpecific: MyType=specific
I'm specific (specific)


Now it is possible to directly return a value of "specific" from MyType (except for Specific1 where it's never assigned and is thus correctly always returned as "generic").

Why is it that explicitly defining Field MyType$ = "specific" doesn't work, while leaving it out does work as I'd think it should?

Update: Is it that if it's defined again, there are two different MyType$s that are accessed depending on the "scope" of the item doing the accessing (TGeneric vs. TSpecific)?


WendellM(Posted 2005) [#2]
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:


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).