Forum > Beginners
Using arrays in routines, etc.
Thaddy:
--- Quote from: KodeZwerg on February 22, 2024, 12:21:00 pm ---Hmmm it accept static or dynamic, whats your point?
--- End quote ---
Your complete misunderstanding that the parameter is an open array and not a dynamic array?
Although it looks the same syntax it means something completely different.
The most important difference is that an open array is of unspecified length, whereas a dynamic array has a specified length at the moment it is passed as parameter..
Because of that, dynamic array parameters can be handled more efficiently by the compiler compared to open array parameters. Its initial length is known.
Hence Marco put you on the right track.
That mistake is quite common. It also has been explained many times on this forum.
Your suggestion of using out is an even bigger mistake, because then the parameter refers to a dynamic array and not to an open array.
Can you still follow me?
KodeZwerg:
--- Quote from: Thaddy on February 23, 2024, 01:50:24 pm ---
--- Quote from: KodeZwerg on February 22, 2024, 12:21:00 pm ---Hmmm it accept static or dynamic, whats your point?
--- End quote ---
Your complete misunderstanding that the parameter is an open array and not a dynamic array?
Although it looks the same syntax it means something completely different.
The most important difference is that an open array is of unspecified length, whereas a dynamic array has a specified length at the moment it is passed as parameter..
Because of that, dynamic array parameters can be handled more efficiently by the compiler compared to open array parameters. Its initial length is known.
Hence Marco put you on the right track.
--- End quote ---
I do know that/understand.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type TArr = array of SomeType; // dynamic function DoSomething(out a: TArr): Boolean;here the compiler knows that it deal with dynamic and not open array type whereas:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function DoSomething(out a: array of SomeType): Boolean;there the compiler treat it as open array, what can be static or dynamic.
Beside of your concerns about efficiency inside the compiler, what is wrong with my given example on how it can be done?
--- Quote from: Thaddy on February 23, 2024, 01:50:24 pm ---Your suggestion of using out is an even bigger mistake, because then the parameter refers to a dynamic array and not to an open array.
Can you still follow me?
--- End quote ---
No I can not but I would be happy to learn to suppress any mistakes in future from my side, thank you very much!
Please use this example to visualize or explain my errors:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$APPTYPE CONSOLE} type TFoo = array of string; // dynamic variantfunction a(out Aa: TFoo): Boolean;begin SetLength(Aa, 5); // hint about it might not been initialized // optional put data in array Result := True; WriteLn('A say length is: ', Length(Aa));end; // open array variant that can consume static or dynamic as long the type matchfunction b(out Aa: array of string): Boolean;begin // optional put data in array Result := True; WriteLn('B say length is: ', Length(Aa));end; var x: TFoo; y: array[0..4] of string;begin a(x); b(y); b(x); ReadLn;end.
Thaddy:
--- Quote from: KodeZwerg on February 23, 2024, 03:02:45 pm ---
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// open array variant that can consume static or dynamic as long the type matchfunction b(out Aa: array of string): Boolean;begin // optional put data in array Result := True; WriteLn('B say length is: ', Length(Aa));end;
--- End quote ---
That can not be an open array parameter but is a dynamic array parameter because of the out modifier.
Open array parameters can have only const or nothing at all. They are also strictly in, although they can be transformed to any array type. as a function result. (At a cost).
As I predicted: you still don't get it. All because of the similarities in syntax.
Consider this old (2019) example of mine on this forum:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}{$H+}type TWordArray = array of word; // open array function ArrayContains(const arr:array of word;item:word):Boolean;overload; var i:integer; begin Result := false; for i :=0 to high(arr) do if arr[i] = item then result := true;;// if arr[i] = item then begin result := true;arr[i]:=100;exit end; // this won't work end; // typed array function ArrayContains(const arr:TWordArray;item:word):Boolean;overload; var i:integer; begin Result := false; for i :=0 to high(arr) do if arr[i] = item then begin result := true;arr[i]:=100;exit end; // but this will allow the modification end; var a:TWordArray = [1,2,3,4,5,6,7,8,9]; b:array of word = [1,2,3,4,5,6,7,8,9];begin writeln(arrayContains(a,3)); writeln(arrayContains(b,3));end.Note the behavior has slightly changed in the 3.2.0 release.
KodeZwerg:
--- Quote from: Thaddy on February 23, 2024, 03:08:26 pm ---As I predicted: you still don't get it. All because of the similarities in syntax.
--- End quote ---
You are probably right :D
I stay with my dynamics and all is good :D
Thank you for explanation and your time! :-*
Thaddy:
What I agree with you is that it is a horribly confusing syntax. :'(
and we will have to explain it over and over again. :'( :'( :'(
Navigation
[0] Message Index
[*] Previous page