I'm just looking for confirmation of my realization that CONST/VAR is useful also for TArray method parameters (not just strings) - before I start changing all my TArray parameters to include CONST or VAR (if changes are needed).

I'm just looking for confirmation of my realization that CONST/VAR is useful also for TArray method parameters (not just strings) - before I start changing all my TArray parameters to include CONST or VAR (if changes are needed).
To some extent, I understand the difference between array of T and TArray
But looking into CPU code, there is a big difference between TArray
This code:
TCarBrand = record
Id: integer;
BrandName: string;
BrandStr: string;
end;
var
Form2: TForm2;
gVal: integer;
myArray: TArray
implementation
{$R *.dfm}
procedure Test1(aArray: TArray
begin// Test1
gVal := aArray[1].Id;// Test1
end;// Test1
procedure Test2(var aArray: TArray
begin// Test2
gVal := aArray[1].Id; // Test2
end;// Test2
procedure Test3(const aArray: TArray
begin// Test3
gVal := aArray[1].Id;// Test3
end;// Test3
procedure TForm2.Button1Click(Sender: TObject);
begin
SetLength(myArray,1);
test1(myArray);
test2(myArray);
test3(myArray);
end;
generates CPU instructions on included image.
So, leaving TArray parameter without CONST or VAR is wasting a lot of resources, correct?
Arioch The I can't make any sense of that. Are you perhaps proposing that two different array of Foo types would be compatible? That would make sense to me.
ReplyDeletePersonally I think this is a compiler glitch as it produces a new type each time it finds "array of x" and due to being a complex type does not make different "array of x" assignment compatible.
ReplyDeletetry this:
var
a, b: array of Byte;
c: array of Byte;
begin
a := b; // works
c := b; // does not work, E2010
You can verify this when you for example call SetLength and then inspect the typeInfo parameter - it will be the same for a and b but different for c.
For other types I would love if Delphi would be stricter and not simply silently allow assigning any ordinal type to others regardless being signed or unsigned and often not even emitting a warning when doing so as well as being able to assign those redeclarations of types. When I have a TFileName I often don't want to directly assign a string to it and cause TFileName to contain invalid characters that make it an invalid filename (yes I know I can make a record type taking care of that but yay, needless overhead galore).
I think treating two "array of x" variables as different types is weird remains of the past century. Let's rememeber back then there was no strings, no dynamic nor open arrays, no generics. For the "school language" it had sense, to enforce a mindset "what you did not explicitly declaree does not exist".
ReplyDeleteBut today in Delphi it is just a gotcha, just a deviation from typical pattern without any practical reason.
Keeping irregularities just because "40 years ago it was so" brings little but harm.
Two distinct "array of x" type variables in practical use should be no different than two "string" or two "TArray" variables.