Recent

Author Topic: Unexpected behavior of TStrings.AddStrings  (Read 3377 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 17451
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: Unexpected behavior of TStrings.AddStrings
« Reply #15 on: February 28, 2022, 11:31:49 am »
I would think that this behaviour (interpreting a string parameter as an open array of string) is a bug.
Of course not: AddStrings indicates an array, even if the array has just one member. So the solution is sheer logical.
So if you would use AddStrings instead of add , an array of string is the correct syntax.[]
For single strings you can just use plain add.
« Last Edit: February 28, 2022, 11:34:02 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

simone

  • Hero Member
  • *****
  • Posts: 668
Re: Unexpected behavior of TStrings.AddStrings
« Reply #16 on: February 28, 2022, 12:16:15 pm »
AddStrings expects an Array of Strings or a TStringList. So it shouldn't accept a string. A string is not (formally) equivalent to an array of strings with length =1. In the example I posted, a string that is not in square brackets is passed to AddStrings.
« Last Edit: February 28, 2022, 01:02:29 pm by simone »
Microsoft Windows 10/11 64 bit - Lazarus 3.8/4.0 FPC 3.2.2 x86_64-win64-win32/win64

PascalDragon

  • Hero Member
  • *****
  • Posts: 6049
  • Compiler Developer
Re: Unexpected behavior of TStrings.AddStrings
« Reply #17 on: February 28, 2022, 02:43:37 pm »
I did not know that when a procedure / function expects an open array parameter, it is possible to invoke it with a scalar value of the same type as the array. In other words, this is possible:

Code: Pascal  [Select][+][-]
  1. program project1;
  2. var
  3.   S : string;
  4.  
  5.   procedure Proc(L : array of string);
  6.   begin
  7.     writeln(Length(L));
  8.     readln;
  9.   end;
  10.  
  11. begin
  12.   Proc(S);
  13. end.

Just for the sake of knowledge: is this behavior explicitly contemplated in the language specification?

While not documented as such, yes, this is explicitly supported. So please file a bug for the documentation of open array parameters that this is missing.

Side note: this functionality works with essentially no effort, because an open array parameter is internally a pointer to the first element of the array plus an additional parameter with the High value. A single element then is simply a pointer to that element and the High parameter has a value of 1. This also explains the error in Delphi that (untyped) constants can't be used: one can't get a pointer to them.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8453
Re: Unexpected behavior of TStrings.AddStrings
« Reply #18 on: February 28, 2022, 03:13:28 pm »
While not documented as such, yes, this is explicitly supported. So please file a bug for the documentation of open array parameters that this is missing.

Thanks for the clarification.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

simone

  • Hero Member
  • *****
  • Posts: 668
Re: Unexpected behavior of TStrings.AddStrings
« Reply #19 on: February 28, 2022, 03:40:16 pm »
@PascalDragon:

Thanks for the in-depth explanations. I imagined that this equivalence was allowed by the internal representation of data structures. This behavior can certainly be convenient in many cases. In my case it caused me some problems during a refactoring process, in which at some point I changed a data structure from array of string to string. The compiler did not complain in front of a procedure that expected a parameter of type array of string and instead was now receiving a string. For this reason it took me a long time to understand that this implicit conversion was the cause of some malfunctions in the software.
Microsoft Windows 10/11 64 bit - Lazarus 3.8/4.0 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018