(As this is related to the other thread, I finally answer this one now ...)
Peter N Lewis wrote:
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.
Yes. Perhaps one solution would be like:
type Foo = array [1 .. MaxVal] of Integer;
const Foo0 = Foo[otherwise 0];
begin p^[a .. b] := Foo0[a .. b]; end;
Now, this wouldn't be EP (EP allows slices only of string variables), and GPC currently doesn't allow it because of strict type-checking (cf. other thread). And if it did, I don't know how efficient code it would generate -- basically we'd need rather good constant folding for slices with possibly non-constant bounds, of constructors. (Waldek, does this sound like horrors to implement? ;-)
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?
Sorry, I don't really like this, for several reasons:
- Introduces a new syntax, quite different from everything existing. What's more like regular syntax would be:
p^[3..6] := [otherwise 5];
I.e., like a structured value, but without a type specifier -- as in: type adjusting to the LHS. But that's exactly what probably would make it hard to implement.
- doesn't specify the bounds,
- loose typing WRT the component type,
- wouldn't solve the problem if the array contents are not scalar.
Frank