Mar 062004

Delphi 7 doesn’t allow me to assign one variable declared as “array of byte” to another variable declared as “array of byte” but Delphi 8 accepts this.

Why is there a difference?

The issue is type identity. Delphi considers the name of a type to be its identity. If two arrays of the same “shape” have different type names, then Delphi does not consider them to be equivalent types. This makes it easy for the programmer to define distinct type domains that happen to be implemented using identical structure. This also means that two variables of type “array of foo” are not considered compatible because they do not share the same symbol name. “array of foo” is an anonymous type, and each anonymous type is unique.

The other kind of type identity is structural identity: if two arrays have the same “shape” (the same number and range of dimensions and the same element type), then they are considered equivalent types. This is the C view of the world.

.NET CLR uses name identity for all types except array (and possibly delegate) types. In CLR, array types cannot be named at all. The only kind of identity for arrays is structural. This is a bit of a hole in CLR’s claim to being a language neutral type system. CLR requires that languages implement structural identity for array types.

So, we ended up softening the Delphi language’s type identity rules a bit to conform to CLR. Delphi will fall back to structural identity comparisons for array types if name identity comparison fails. This applies only to array types.

Thus, Delphi 8 for .NET will accept the following code but Delphi 7 will not:

  x: array of byte; 
  y: array of byte: 
  x := y; // error in D7, not in D8

Will this concession to structural identity migrate to the Win32 compiler in the next release of Win32 Delphi?

Uncertain. Reluctantly, probably. I’d prefer to keep the crisp, clean, clear lines of name identity, but the goal of improving source code compatibility between the platforms may force the issue.

No problem, just make structural compatibility configurable with a compiler switch!

No thanks. Too many options rot the brain. Maybe one giant gang switch to turn on or off extended syntax (ala $X+ from the D1 days), but not a separate compiler switch for every specific behavior.