At 13:06 +0200 8/9/05, Frank Heckenbach wrote:
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?
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).
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) ...
I'm not certain it is absolutely required, but it's another way we can get to a nice high level construct that avoids doing low level memory tricks. I think that's worth something, but I'm not the one who will have to implement it.
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?
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.
I certainly agree with your negatives, that's why I'd like to know a better way.
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 ...
Having to define a matching type would be impractical as well, especially for the case where 3 and 6 are run time calculations and the assignment might be in the middle of the procedure when the values had been calculated.
I wonder if the case of assigning a single value to an array[x..y] of scalar is sufficiently common that it would be worth a specific solution? That is, we could potentially define some construct (I have no particular suggestion for syntax perhaps array(5)?) which would repesent an arbitrary array of a signle scalar. It would be type compatible with a one dimensional array of any size of scalar which is assignment compatible with the value?
Then p^[3..6] := array(0);
I don't know if this is hard or easy, or worth doing as a GPC only feature or not. The big plus is it is one more way to avoid low level junk like FillChar, especially for the case of zeroing scalars.
Just a thought, Peter.