The same inheritance of a helper entry from two different subtypes

Having a type declaration with two subtypes derived from the same internal type, how can we have a different record helper for each subtype?

Example:

type SingleA = Single; SingleB = Single; _SingleA = record helper for SingleA procedure DoThingsToA; end; _SingleB = record helper for SingleB procedure DoThingsToB; end; 

if I declare var of type SingleA, I always get an helper from type SingleB, I know that this is normal behavior if we redefine the same internal type, but why does this happen with different types?

Any help is much appreciated ...

Thanks in advance.

+5
source share
2 answers

You do not declare subtypes here. This code:

 type SingleA = Single; SingleB = Single; 

It is simply a declaration of aliases , not types . Thus, SingleA and SingleB are actually the same type of Single .

This is explained here:

Type Compatibility and Identification (Delphi)

When one type of identifier is declared using another type identifier, without qualification, they designate the same type. Thus, given the declarations:

 type T1 = Integer; T2 = T1; T3 = Integer; T4 = T2; 

T1 , T2 , T3 , T4 and Integer all designate the same type. To create different types, repeat the word type in the declaration. For instance:

 type TMyInteger = type Integer; 

creates a new type called TMyInteger , which is not identical to Integer .

Essentially, the construction = type x creates new type information for the type, so TypeInfo(SingleA) <> TypeInfo(SingleB) .

In the source code, you simply declare two aliases for the same type of Single .

For any given type (and its aliases), you can have only one type helper in scope , so in your source code, record helper for SingleB hides record helper for SingleA .

By updating aliases to their own types, you avoid this problem:

 type SingleA = type Single; SingleB = type Single; <<-- the type keyword makes SingleB distinct from SingleA 

You will now have two different types, and your recording helpers will work as expected.

+7
source

Sorry when you declare

 type SingleA = Single; 

Delphi treats this as an alias, not a derived type.

Thus, he sees in your case SingleA, SingleB and Single, since they are all identical. (This can be seen by declaring a function with a parameter of type SingleA and trying to pass it a parameter of SingleB).

So, following the rule that for any one type there can be only one helper, and the helper for SingleB was the last, then, as you comment, the behavior should be expected.

Edit

You can also see this by going through your code and looking at the parameter type of a variable of type SingleA or SingleB in a local variable window, an instance of foe. You will see that it is always specified as a type of Single.

+1
source

All Articles