Feature request:a way of declaring a type that is an alias of a currently known type BUT is NOT assignment compatible with its base type. the following example uses a new keyword "
distinct" to inform the compiler the new type is not assignment compatible with its base type.
type
TMYINTEGER = distinct integer;
var
a : integer;
b : TMYINTEGER = TMYINTEGER(0); { MUST be typecasted }
c : TMYINTEGER = integer(0); { error because a TMYINTEGER is not assignment compatible with integer }
begin
a := integer(b); { ok }
b := TMYINTEGER(a); { ok }
b := 0; { NOT ok -> error incompatible types }
b := TMYINTEGER(ord('z')); { ok, ord returns an integer which is then cast }
b := TMYINTEGER('z'); { NOT ok -> sizeof('z') = 1 <> sizeof(TMYINTEGER) }
b := TMYINTEGER('abcd'); { ok, 4 characters -> size 4 -> same size as TMYINTEGER }
end;
a typecast should be considered valid ONLY when type size of the types involved is the same.
NOTE: this is a standard feature in Ada.
Implementation is trivial and, simpler than what FPC currently does. The compiler should simply consider variables of distinct types to be assignment compatible only when the types are the same just as it does for records. IOW, the compiler could "think" of the type as a record type as far as assignment compatibility is concerned.
I suggest "distinct" as a new keyword to differentiate types but, anything that implies uniqueness would likely be another reasonable option.
Why have this ?: many, many reasons, but among them, that would allow creating Windows API definitions where the type HWND isn't compatible with HHEAP or HMODULE or HANYOTHERHANDLE which would enable the compiler to do type checking (after all, Pascal is supposed to do strong type checking.)
NOTE: a possible alternative to "
distinct" would be "
strict".
Additional feature request:Consider casts of equal size entities to be valid in const declaration. Consider the following example:
const
ASIGNATURE = DWORD('abcd');
Currently FPC will not accept the above constant. Since 'abcd' is 4 bytes, the size of a DWORD and a DWORD is a valid constant, it would be very nice, not to mention convenient, if the compiler accepted such casts.
It is also very commonly use to create record signatures. MZ anybody ?
