Recent

Author Topic: [Solved] Relationship between high(ashortstring) and length(ashortstring)  (Read 8677 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3873
    • StrumPract is the musicians best friend
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #60 on: September 12, 2022, 05:46:22 pm »
The community edition is free for everyone available, but has some minor limitations and is restricted to terms from embarcadero.

Thanks for the info but, for developing, I use exclusively Open Source projects, including the compiler and OS.  O:-)
Also I dont have Windows installed, so I thank you again to have done the tests for me.

« Last Edit: September 12, 2022, 06:27:48 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #61 on: September 12, 2022, 06:22:06 pm »
The community edition is free for everyone available, but has some minor limitations and is restricted to terms from embarcadero.
One of the biggest limitations... no support here  :P  :D

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #62 on: September 12, 2022, 06:39:41 pm »
that should mean there is something really wrong with old WideString. What was that wrong, that precluded WideString name reuse??? At least that was how i learnt of the difference.
Main difference is that UnicodeString is reference counted whilst WideString not.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #63 on: September 12, 2022, 06:41:02 pm »
Many thanks KodeZwerg, it was a hard battle but we won.  ;D
You are welcome, now lets go dance together  :D :D :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #64 on: September 12, 2022, 06:45:19 pm »
The community edition is free for everyone available, but has some minor limitations and is restricted to terms from embarcadero.
One of the biggest limitations... no support here  :P  :D
Lets skip the Delphi talk and focus on the real thing  O:-) 8-)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #65 on: September 12, 2022, 07:06:09 pm »
that should mean there is something really wrong with old WideString. What was that wrong, that precluded WideString name reuse??? At least that was how i learnt of the difference.
Main difference is that UnicodeString is reference counted whilst WideString not.

Man, you just again repeated what i just said above, this time https://forum.lazarus.freepascal.org/index.php/topic,60528.msg453229.html#msg453229

It seems like you only read one last message , rather than the whole thread :-(

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #66 on: September 12, 2022, 07:27:44 pm »
Okay, I am sorry to disappoint you by repeating what you have written and apology for repeating what you have mentioned.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6381
  • Compiler Developer
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #67 on: September 13, 2022, 09:19:43 am »
Almost. FPC defaults to OBJFPC and following that, String defaults to ShortString.

As Fred mentioned, the default mode of FPC is FPC, not ObjFPC.

Delphi uses alias String for AnsiString
True?
No and Yes.
Delphi 2009 and newer = string = WideString
Delphi lower than 2009 = string = AnsiString

I hope this helps but in fact it makes no difference on your subject.

The point was that Delphi default to not ShortString (aka {$H+}).

However, i expected that FPC get rid of it. Delphi has no difference between out-params and var-params, at least for strings. FPC did make a technical difference. I was expecting FPC fixed it. Afterall bug-compatibility was not FPC's goal, was it...

I don't get what exactly you mean here?

The community edition is free for everyone available, but has some minor limitations and is restricted to terms from embarcadero.

Worst of all, it does not have sources (or at least used to not have) and for me it means i can never rely on it.
When it would find some bug in the libraries (maybe tirggered by compiler bug, as was the case in Delphi 4.0) - i would not be able to research it, and would be able to neither patch the libraries nor to define and apply workaround.

Granted, to test which variables are initialized and which are not sources-less edition would suit. But for any serious work it would be poor choice.

For us compiler developers the editions without source are perfect, cause we can't be tainted by reading the Delphi code then.

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #68 on: September 13, 2022, 09:37:18 pm »
i mean exactly the coan of passing Result into function.

Code: Pascal  [Select][+][-]
  1. function Invert(const n: integer): string;
  2. var i: integer;
  3. begin
  4.   for i := 1 to n do writeln(Result);
  5. end;
  6.  
  7. var coan: string;
  8. begin
  9.    coan := 'Impossible!';
  10.    coan := Invert(10);
  11. end.  
  12.  

This misbehaviour happenned because Delphi did not have out-parameters for many years. Only var parameters.

When out-parameters were added to the syntax, they still were not ocmpletely implemented for all types. For long strings AFAIR out parameters is just a shortcut for var-parameter.

Because string Result in Delphi is var-parameter instead of out-parameter - there were no out-parameters when funcitons were being implemented in Delphi compiler -  this coan is works, against common sense and at times leading to errors.

But is FPC aimed at bug-compatibiltiy with Delphi or feature-compatibility?

Why can not FPC make Result of funcitons out-parameteer instead of var-parameter?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12766
  • FPC developer.
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #69 on: September 13, 2022, 10:57:24 pm »
Code: Pascal  [Select][+][-]
  1. function Invert(const n: integer): string;
  2. var i: integer;
  3. begin
  4.   for i := 1 to n do writeln(Result);
  5. end;
  6.  

Illegal code, result is never assigned.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6381
  • Compiler Developer
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #70 on: September 14, 2022, 09:48:12 am »
When out-parameters were added to the syntax, they still were not ocmpletely implemented for all types. For long strings AFAIR out parameters is just a shortcut for var-parameter.

No, out-parameters of managed types lead to the finalization of the corresponding variable while var does not.

But is FPC aimed at bug-compatibiltiy with Delphi or feature-compatibility?

Regarding the calling ABI we're indeed trying for compatibility, because user code might rely on this.

Why can not FPC make Result of funcitons out-parameteer instead of var-parameter?

First of, since this behaviour exists for many years already, we'll not change this, cause existing code (for example the RTL itself) relies on this behaviour, not to mention that it's documented as such.

Second this would result in a performance impact, cause the corresponding variable would be finalized even if it might not be needed.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8563
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #71 on: September 14, 2022, 10:45:56 am »
Are you sure this is a link to the official documentation?

GetLazarus is definitely not official, even though it appears to be mirroring the official documentation.

Particularly since they appear to be running their own competing forums (also based on SMF) I am very concerned that this is "diluting the brand" by making it more difficult for inexperienced users to find authoritative information.



The official documentation pages are at

https://www.freepascal.org/docs.html

and

https://lazarus-ccr.sourceforge.io/docs/

The distinction being that the first of those covers the current version of FPC, while the second covers the version of FPC bundled with Lazarus which might differ.



Now could the maintainers please get their collective arse in gear and sort out a working URL for the PDFs?

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

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #72 on: September 15, 2022, 08:17:56 pm »
No, out-parameters of managed types lead to the finalization of the corresponding variable while var does not.

In Delphi it does not, with long strings. It is inconsistent and frankly is a bug.

But is FPC aimed at bug-compatibiltiy with Delphi or feature-compatibility?

Regarding the calling ABI we're indeed trying for compatibility, because user code might rely on this.

User code might rely on `Result` var value implicitly passed form caller to callee??? Not a hackerish showing off, but actual production code?

Ж8-[...]

Personally I, when developing apps, was only hit by the opposite, when i was forgetting to set Result in some code paths, and the compiler did not throw a error at me. It was a type bugs rather hard to reproduce and find :-(

Why can not FPC make Result of funcitons out-parameteer instead of var-parameter?
...cause existing code (for example the RTL itself) relies on this behaviour

RTL relies upon Result var pre-initialized with value??? Relies on a de facto compiler bug, albeit old one? I don't want to believe it.

Quote
Second this would result in a performance impact, cause the corresponding variable would be finalized even if it might not be needed.

There is always a code flow analysis w.r.t. "use of uninitialized variable" - it can be used to choose if to pre-Finalize or not. This would be some complication, but not much.

And i do not think "pre-Finalize always" would have an impact really. You do finalizing anyway when you assign something to Result. So it would be just split into two operations. And the assignments then would only have one nil-check added to the flow. Compared to the asisgnment itself (CPU bus locking for ARC) it does not seem much delay adding one extra nil-comparison to the function control flow.

The only real performance impact would be when developer forgot to assign Result at all, but in my view that constitutes a bug that should be fixed anyway. However, you seem to think that intentional non-assigning Result for long strings is a valid implementation of functions.
« Last Edit: September 15, 2022, 08:20:27 pm by Arioch »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6381
  • Compiler Developer
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #73 on: September 16, 2022, 04:04:12 pm »
Why can not FPC make Result of funcitons out-parameteer instead of var-parameter?
...cause existing code (for example the RTL itself) relies on this behaviour

RTL relies upon Result var pre-initialized with value??? Relies on a de facto compiler bug, albeit old one? I don't want to believe it.

The compiler explicitly passes the result value like that. This is by design and not considered a bug.

Again: we will not change how managed result values are passed.

 

TinyPortal © 2005-2018