Lazarus

Programming => General => Topic started by: Packs on November 28, 2024, 06:31:44 am

Title: In pascal multiple line string possible
Post by: Packs on November 28, 2024, 06:31:44 am
In pascal multiple line string.
Title: Re: In pascal multiple line string possible
Post by: Handoko on November 28, 2024, 06:55:12 am
Did you mean this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   S: string;
  4. begin
  5.   S := 'This is a'            + LineEnding +
  6.        'multiple line string' + LineEnding +
  7.        'in Pascal.';
  8.   ShowMessage(S);
  9. end;
Title: Re: In pascal multiple line string possible
Post by: Packs on November 28, 2024, 07:03:06 am
Ok.

Thank you 🙏
Title: Re: In pascal multiple line string possible
Post by: Warfley on November 28, 2024, 11:10:50 am
If I have to write a lot of lines I personally use a macro:
Code: Pascal  [Select][+][-]
  1. {$Macro On}
  2. {$Define ln:=+LineEnding+}
  3.  
  4. const
  5.   MyString =
  6.     'Line1'ln
  7.     'Line2'ln
  8.     'Line3'ln
  9.     'Line3';
Title: Re: In pascal multiple line string possible
Post by: Handoko on November 28, 2024, 11:14:52 am
Nice trick!
Title: Re: In pascal multiple line string possible
Post by: Packs on November 28, 2024, 11:35:40 am
Thank you 🙏
Title: Re: In pascal multiple line string possible
Post by: Thaddy on November 28, 2024, 12:01:15 pm
I would not use that macro with that name, since ln is also a system function.
Macro fine, name not. Try al or something.
Title: Re: In pascal multiple line string possible
Post by: Warfley on November 28, 2024, 12:45:31 pm
Usually when I'm at the point where I need extended string constants in my code, I often put them in their own unit, with no code, so the macros don't really matter at all.

But yeah, generally short macros can be dangerous as its likely they collide with other existing names, an alternative is to undef the macro after use
Title: Re: In pascal multiple line string possible
Post by: Thaddy on November 28, 2024, 12:52:10 pm
Usually I use an include. Similar.
Title: Re: In pascal multiple line string possible
Post by: LV on November 28, 2024, 01:06:22 pm
#13#10(win) = #10(Linux) = #13(MacOS) = sLineBreak

https://forum.lazarus.freepascal.org/index.php?topic=57659.0

syswinh.inc  ;)

Code: Pascal  [Select][+][-]
  1. const
  2.  LineEnding = #13#10;
  3. ...
  4. sLineBreak = LineEnding;
  5.  
Title: Re: In pascal multiple line string possible
Post by: jamie on November 28, 2024, 01:15:30 pm
If I have to write a lot of lines I personally use a macro:
Code: Pascal  [Select][+][-]
  1. {$Macro On}
  2. {$Define ln:=+LineEnding+}
  3.  
  4. const
  5.   MyString =
  6.     'Line1'ln
  7.     'Line2'ln
  8.     'Line3'ln
  9.     'Line3';

Hmm, "ln" = natural logarithm

Jamie
Title: Re: In pascal multiple line string possible
Post by: Thaddy on November 28, 2024, 01:46:06 pm
Warfley already answered that and added an alternative.
Title: Re: In pascal multiple line string possible
Post by: Remy Lebeau on November 28, 2024, 06:47:08 pm
Delphi 12.0 introduced Multi-line String Literals (https://docwiki.embarcadero.com/RADStudio/en/String_Types_(Delphi)#Long_and_Multiline_String_Literals), for example:

Code: Pascal  [Select][+][-]
  1. const
  2.   MyString = '''
  3.    Line1
  4.    Line2
  5.    Line3
  6.    Line4
  7.  ''';

Maybe FreePascal will add something like this someday.
Title: Re: In pascal multiple line string possible
Post by: Thaddy on November 28, 2024, 09:20:15 pm
There is actually a patch for that, that I tested and confirmed to work, but it used back-ticks..
It is horrible, though. Refused for all the right reasons. But the code is available and working (syntax being not relevant to implementation)
The Delphi case should be refused too...
I have inline var syndrome, mind... Silly idiots.
Title: Re: In pascal multiple line string possible
Post by: MarkMLl on November 28, 2024, 09:57:15 pm
There is actually a patch for that, that I tested and confirmed to work, but it used back-ticks..

Which visually is probably not a bad choice: sequences of '""' etc. "get old fast".

I know that the core team is inclined to tell us to put multilines into resource files, but I maintain- impenitently and from experience- that they are wrong. If you're writing Pascal code that relies on- for example- an SQL fragment, the place to put that fragment is adjacent to the associated Pascal: and that's also the place to put the comments explaining what the combination is doing.

Unless, of course, somebody would like to argue that comments should be moved out of the unit and into a file of their own?

MarkMLl
Title: Re: In pascal multiple line string possible
Post by: TRon on November 28, 2024, 10:00:26 pm
Unless, of course, somebody would like to argue that comments should be moved out of the unit and into a file of their own?
They should be put in the cloud or better yet, ai generated. Now let's try that with some SQL  :D
Title: Re: In pascal multiple line string possible
Post by: VisualLab on November 28, 2024, 10:43:58 pm
I have inline var syndrome, mind... Silly idiots.

Then are there also smart idiots? :)



I understand your irritation towards Delphi developers. But in Delphi, the following are much more irritating (for example):

I bet these shortcomings are the result of management decisions, not developers.
Title: Re: In pascal multiple line string possible
Post by: TRon on November 28, 2024, 10:47:10 pm
Then are there also smart idiots? :)
That is named idiot savant (https://en.wiktionary.org/wiki/idiot_savant)  :)
Title: Re: In pascal multiple line string possible
Post by: PierceNg on November 29, 2024, 03:18:47 am
I understand your irritation towards Delphi developers. But in Delphi, the following are much more irritating (for example):

The most irritating for me (Delphi CE 11 and 12) is the need for my account to be administrator to install/patch Delphi. Not "run as administrator", the account must be administrator. As a non-administrator, I can run Delphi, write programs, but installing patches just goes through the motion and then when I restart Delphi, "patches avaiable".
Title: Re: In pascal multiple line string possible
Post by: myisjwj on November 29, 2024, 03:33:14 am
temp:=String.Join(sLineBreak, [
'SELECT *',
'FROM Customers',
'WHERE Department = ''Kinglandsoft''',
'ORDER BY Name'
]);
Title: Re: In pascal multiple line string possible
Post by: Packs on November 30, 2024, 11:45:07 am
Thank you every one .
Title: Re: In pascal multiple line string possible
Post by: Warfley on November 30, 2024, 12:21:13 pm
Delphi 12.0 introduced Multi-line String Literals (https://docwiki.embarcadero.com/RADStudio/en/String_Types_(Delphi)#Long_and_Multiline_String_Literals), for example:

Code: Pascal  [Select][+][-]
  1. const
  2.   MyString = '''
  3.    Line1
  4.    Line2
  5.    Line3
  6.    Line4
  7.  ''';

Maybe FreePascal will add something like this someday.

I personally do not like multi line strings because they break code indentation. I haven't used delphi, if you'd did something like this in python your or C#, each line has a 4 space prefix. So if you want to print the text you first need to do preprocessing of the string to remove those spaces.
The alternative is to write text unindented:
Code: Pascal  [Select][+][-]
  1. const
  2.   MyString = '''Line1
  3. Line2
  4. Line3'''
.
While for like global constants (like i18 strings) this may be acceptable to some degree, having this somewhere in already nested code completely breaks the visual structure of the code.
Title: Re: In pascal multiple line string possible
Post by: Packs on November 30, 2024, 01:52:44 pm
in golang

str := `I
am
a
multiline
string`

for sql it should automatically beautify . ie will be great feature 
Title: Re: In pascal multiple line string possible
Post by: marcov on November 30, 2024, 02:53:47 pm
The Delphi case should be refused too...
I have inline var syndrome, mind... Silly idiots.

You mean language design by mob rule ?
Title: Re: In Pascal multiple line string possible
Post by: Kays on November 30, 2024, 03:10:37 pm
No, multi‑line strings are by definition not possible. A string is a sequence of char values along one dimension. You can access individual char components with the bracket syntax myStringVariable[123].

A “multi‑line string” proper would mean you could access lines and characters in a line individually, like myStringVariable[lineIndex] would yield one line and myStringVariable[lineIndex][charIndex] would yield a char value within that line.

The only thing that is possible is – as it has been shown – to embed end‑of‑line character sequences within a string, but that does not make the string multi‑dimensional, a data type arranging character sequences into lines. It is still single‑dimensioned.

Code: Pascal  [Select][+][-]
  1. []
  2.   S := 'This is a'            + LineEnding +
  3.        'multiple line string' + LineEnding +
  4.        'in Pascal.';
  5. []
[…]
[…]
NB: This makes the last “line” a degenerate line, an incomplete line. A line is a (possibly empty) sequence of char values (except those of the end‑of‑line character sequence) followed by one end‑of‑line character sequence.
Title: Re: In pascal multiple line string possible
Post by: Zoran on November 30, 2024, 03:57:52 pm
Lazarus provides a very nice way for entering multiline strings.
Seems that not many people use it just because the name of the menu item in the IDE is not very intuitive.
The details: https://forum.lazarus.freepascal.org/index.php/topic,52669.msg388780.html#msg388780 (https://forum.lazarus.freepascal.org/index.php/topic,52669.msg388780.html#msg388780)
Title: Re: In Pascal multiple line string possible
Post by: MarkMLl on November 30, 2024, 04:27:40 pm
No, multi‑line strings are by definition not possible. A string is a sequence of char values along one dimension.

Yes, and once you've progressed beyond punched cards and mainframe fixed-length records an EOL is just an embedded character.

At that point you either need some internal convention to quote it (e.g. implicit concatenation where quoted strings are separated only by whitespace), an escaping mechanism (e.g. \ ) or a convention that results in an internal EOL being interpreted as part of the string rather than dividing multiple lines of sourcecode.

There's been conventions for embedding SQL etc. in sourcecode since the 1980s. It's high time that Pascal woke up to the fact.

MarkMLl
Title: Re: In pascal multiple line string possible
Post by: Thaddy on November 30, 2024, 04:46:56 pm
Well:
https://gitlab.com/freepascal.org/fpc/source/-/issues/35827
tested and works.
If I want it? No, but it works and as I wrote before, the code is available but not applied.
(you need to patch your own compiler version)

It seems several people have been sleeping for a couple of years.
Quote
No, multi‑line strings are by definition not possible.
is talking nonsense.
Note the Delphi syntax is, however horrid, slightly more elegant.

That patch could be applied with the same syntax '''
Title: Re: In pascal multiple line string possible
Post by: Tony Stone on November 30, 2024, 05:09:27 pm
Lazarus provides a very nice way for entering multiline strings.
Seems that not many people use it just because the name of the menu item in the IDE is not very intuitive.
The details: https://forum.lazarus.freepascal.org/index.php/topic,52669.msg388780.html#msg388780 (https://forum.lazarus.freepascal.org/index.php/topic,52669.msg388780.html#msg388780)




WHAT THE HECK!  I knew a while back I saw something in Lazarus to edit multi line strings!  A couple weeks ago I really needed it so I wrote my own tool!!  Dam it.  WHAT A HORRIBLE NAME!!! I looked through the IDE for a good 10 minutes looking for it then gave up to write my own. lol!

Here is what I wrote... kind of useless now haha https://github.com/TonyStone31/Pascal-MultiLine-String-Helper (https://github.com/TonyStone31/Pascal-MultiLine-String-Helper)  (oh well... was kind of fun to cobble together)
Title: Re: In pascal multiple line string possible
Post by: PascalDragon on November 30, 2024, 06:08:21 pm
There is actually a patch for that, that I tested and confirmed to work, but it used back-ticks..

Which visually is probably not a bad choice: sequences of '""' etc. "get old fast".

I know that the core team is inclined to tell us to put multilines into resource files, but I maintain- impenitently and from experience- that they are wrong. If you're writing Pascal code that relies on- for example- an SQL fragment, the place to put that fragment is adjacent to the associated Pascal: and that's also the place to put the comments explaining what the combination is doing.

Unless, of course, somebody would like to argue that comments should be moved out of the unit and into a file of their own?

Comments and embedded fragments of other languages are different things, the former are part of the language, the later are simply strings from the language's point of view.

For code fragments moving them to a separate file has the advantage that an IDE can easily syntax highlight them or provide refactoring features. With code embedded as part of other code this becomes harder.
Title: Re: In pascal multiple line string possible
Post by: MarkMLl on December 01, 2024, 09:44:04 am
For code fragments moving them to a separate file has the advantage that an IDE can easily syntax highlight them or provide refactoring features. With code embedded as part of other code this becomes harder.

I don't think it's reasonable to design a language with the express intention of making it easy for IDEs. Declarations/definitions should be relatively close to the location at which they're used, and forcing a reader/maintainer to consult material in a separate file is as bad as forcing him to flip back to the top of the current one: and almost as bad as allowing such things to be added as footnotes after the point at which they're used.

My own opinion is that there is no need for an embedded fragment to be highlighted as anything other than an alien syntax or for the content to be refactored, in the same way that there is no need for the IDE to offer a debugger for it: it's reasonable to expect the writer to have debugged it elsewhere (although a substantial number of SQL queries in this forum quite obviously haven't been). But in any case this isn't the 1970s any more, and a development environment will happily open multiple files and display each in multiple contexts.

MarkMLl
TinyPortal © 2005-2018