[fpc-pascal] casting back a pointer to original type --> variable & value types

spir ☣ denis.spir at gmail.com
Sun May 30 13:00:37 CEST 2010


This is a follow-up to the previous thread.

Say I put in a TFPList elements of types C C1 C2, where C is a super-class. When I retrieve an element using
var element : C;
    ...
    element := C(list[index]);
    text := element.text;
    ...
what is the actual type of element? and of its value? Concretely, if the originally inserted element was of type C1, C1.text is called -- this is the point discussed & solved in the previous thread.

So, it seems the language knows (at runtime) about the real type of a *value*. I must declare element, the variable, with type C, since I do not know anything at coding time. But Pascal correctly calls C1.text, due to "virtual" & "override" modifiers. So, it means that the language has additional type information and uses it to make a distinction between variable and value types. Correct? Else, how can it correctly call C1.text?
But I'm not 100% sure this means that, for Pascal and at runtime, element's value is of type C1. Is there a way to know value and/or variable's types, such as a func TypeName()?

(Side-note: I do not understand why this overriding mechanism needs to be explicitely stated with a modifier keyword: when a programmer writes a specialised method on a sub-class, obviously s/he intends it to be called, no?)

As you have certainly noted ;-), I have very limited knowledge about static languages. But I thought there were only variable types (at least in Pascal and C). While in dynamic languages there are only value types (and in fact there is no notion of variable like in Pascal, the name:value relation is different). But the case above contradicts this simplistic POV of mine.

Can I rely on this mechanism to correctly dispatch method calls in any case, by building a system such as:
* All values are class instances.
* They have a unique ancestor class C, on which all possibly overridable methods are declared virtual.
* On sub-classes, all methods existing on the ancestor(s) are explicitely declared override.
Then, whatever the class of a variable or of its value for Pascal, the correct method will be called?
If yes, as I think now, this is a very constructive point :-)
Another one would be to acually know a value's type. How to get its name or, much better, a pointer to it? (Since the language obviously has such a pointer.)


Denis
________________________________

vit esse estrany ☣

spir.wikidot.com



More information about the fpc-pascal mailing list