Peter N Lewis wrote:
type MyArray(count: Integer) = array[1..count] of Integer; var p: ^MyArray; begin New(p,10); p^[1..5] := p^[2..6]; end.
However, this might be difficult to implement, because which type should an array slice have? For string slices, we have the generic string-type which is always compatible. For other arrays, there are no generic types, and two arrays [1 .. 5] and [2 .. 6] are normally not compatible. I suppose the Borland solution would be to shift slices (if they had them) to base 0, as they do with arrays in several other contexts, but I don't really like this (as it can be very confusing and a trap to the unknowing, as are their "open arrays"). So if we can find a more elegant solution, without doing away with type-checking at all, of course, it could be worth changing it in GPC ...
I guess the most elegant solution would be to say that p[1..5] is a subarray of MyArray, and that such a type is assignment compatible with MyArray and other subarrays, and that you then need a runtime check to test that the length of the subarrays match (or the subarray matches the total length of MyArray) (unless the values are all compile time constants and the check can be done at runtime, which is not guaranteed).
OK, this check would be much the same as is necessary for schema discriminants (e.g., when either or both of the formal and actual parameters of a schematic type have non compile time constant discriminants). I've implemented this in GPC (20050312 ;-), so we could probably reuse it.
What's more effort is probably the introduction of a new class of type "subarray of something". I'd really have to see how difficult this would be to implement (if we really want that) ...
While I'm at it, is there any better way to zero part of such an
array than:
FillChar( p^[3], 4 * SizeOf(p^[3]), 0 );
I suppose a simple for-loop is not acceptable for some reason?
No particular reason, except for the extra lines, extra index variable and definition, and the I find the for loop is less clearly an array assignment that a Move/Zero. Conceptually to me, the for loop is indexing over the elements of the array, where really I want to reset the array - its a subtle distinction that probably doesn't concern others).
As a mathematician, I can understand the difference, and insofar I tend to agree with you. On the other hand, I have to point out that Move/FillChar are low-level, non-standard, sometimes dangerous (think of strings or schemata in the array) constructs, in comparison to a for-loop, and to me this disadvantage weighs heavier.
If we had a way to do the same thing cleanly, say by assigning an array constructor, this might be more interesting. However, GPC currently doesn't accept it, and I think it's not correct EP either (arrays not compatible, even if structurally equivalent), is it?
program Bar; type MyArray(count: Integer) = array[1..count] of Integer; Foo = array [3 .. 6] of Integer; var p: ^MyArray; begin New(p,10); p^[3 .. 6] := Foo[otherwise 0]; end.
Perhaps we could allow for an extension here to make this work (with much the same issues as above). But I'm still usure how much effort it will be and whether it will be worth it ...
Frank