Recent

Author Topic: Idea of multi-line strings, mantis 35827  (Read 9948 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2365
    • UVviewsoft
Idea of multi-line strings, mantis 35827
« on: December 27, 2020, 04:02:37 pm »
https://bugs.freepascal.org/view.php?id=35827
I only want to suggest a syntax for multi-line strings:

case1)

Code: Pascal  [Select][+][-]
  1. var s = 'begin'/
  2. 'mdl'/
  3. 'end.' ;

Here we use / which is not defined for strings.

case2)

Code: Pascal  [Select][+][-]
  1. var s = "begin
  2. mdl
  3. end" ;

Here we use dbl-quotes.
« Last Edit: December 27, 2020, 04:05:07 pm by Alextp »

AlexTP

  • Hero Member
  • *****
  • Posts: 2365
    • UVviewsoft
Re: Idea of multi-line strings, mantis 35827
« Reply #1 on: December 27, 2020, 04:10:15 pm »
(I edited the post after 5 minutes of thinking)

circular

  • Hero Member
  • *****
  • Posts: 4181
    • Personal webpage
Re: Idea of multi-line strings, mantis 35827
« Reply #2 on: December 27, 2020, 04:18:49 pm »
I like the double quotes version.

About using "/" it seems to be redundant with "+".
Conscience is the debugger of the mind

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Idea of multi-line strings, mantis 35827
« Reply #3 on: December 27, 2020, 04:23:56 pm »
I think that + already works. However if it were my project:

* I'd add _ as a concatenation operator for strings and dynamic arrays, so that it was possible to redefine + as vector addition.

* I'd definitely not mess around with " since in scripting languages I've allowed stings defined with different types of quotes to have different properties (e.g. a string quoted by / to be a regex or backref).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

circular

  • Hero Member
  • *****
  • Posts: 4181
    • Personal webpage
Re: Idea of multi-line strings, mantis 35827
« Reply #4 on: December 27, 2020, 04:36:40 pm »
About the vector addition, that's another (interesting) topic.

I don't see how the usage you do in scripting languages interferes here. It is not the same interpreter.

Another approach is to allow strings to be multiline.
Conscience is the debugger of the mind

AlexTP

  • Hero Member
  • *****
  • Posts: 2365
    • UVviewsoft
Re: Idea of multi-line strings, mantis 35827
« Reply #5 on: December 27, 2020, 04:41:18 pm »
case3)
Like in Python with prefix char 'r', here with 'm':

Code: Pascal  [Select][+][-]
  1. var s = m'begin
  2. mdl
  3. end' ;
  4.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Idea of multi-line strings, mantis 35827
« Reply #6 on: December 27, 2020, 05:55:02 pm »
I don't see how the usage you do in scripting languages interferes here. It is not the same interpreter.

Easily-entered characters are in short supply and shouldn't be squandered, I gave one example of how additional quote types could be beneficial.

Another would be if single-quoted strings defaulted to single-byte characters, and double-quoted strings to Unicode.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Idea of multi-line strings, mantis 35827
« Reply #7 on: December 27, 2020, 09:08:19 pm »
Better load them from resources, easier to post compilation translate them

Zoran

  • Hero Member
  • *****
  • Posts: 1824
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Idea of multi-line strings, mantis 35827
« Reply #8 on: December 27, 2020, 09:33:59 pm »
There was an effort to introduce multiline string, I have no idea what happened with that.

I strongly disagree with that idea, for reasons I gave here: https://forum.lazarus.freepascal.org/index.php/topic,46050.msg330104.html#msg330104

circular

  • Hero Member
  • *****
  • Posts: 4181
    • Personal webpage
Re: Idea of multi-line strings, mantis 35827
« Reply #9 on: December 27, 2020, 09:57:10 pm »
Better load them from resources, easier to post compilation translate them
How would you do that actually?
Conscience is the debugger of the mind

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Idea of multi-line strings, mantis 35827
« Reply #10 on: December 27, 2020, 10:09:12 pm »
Better load them from resources, easier to post compilation translate them
How would you do that actually?

I'm not a core developer, but I think a good case could be made for something handled at the same level as the rest of the conditional directives particularly for stuff like SQL:

const
  myQuery=
{$' terminator
Stuff here
terminator }

But I really don't like screwing the core language any further for this sort of thing.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Idea of multi-line strings, mantis 35827
« Reply #11 on: December 27, 2020, 10:53:30 pm »
With the double quotes there is one problem, it breaks indentation because the indent would be part of the string. And while as functions in variables this might not look so bad, look at this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TTestClass = class
  3.   public const
  4.     Description = "This
  5. is
  6. a
  7. multiline
  8. description";
  9.     SomethingElse = 42;
  10. ...
personally I think this is really ugly.

There is also the question should multiline strings contain the linebreaks or not?
If it shouldn't i.e. just be a convinient way to chop up a string to large for a sinlge line, there is nothing wrong with the + syntax:
Code: Pascal  [Select][+][-]
  1. var
  2.   mls = "multi"+
  3.         "line" +
  4.         "string";
If it should, then imho a simple newline symbol would make more sense. Most languages use escape characters like \n like in C#:
Code: C  [Select][+][-]
  1. String mls = "this is a\n" +
  2.              "multiline\n" +
  3.              "string";
In pascal we have the #num for directly appending chars:
Code: Pascal  [Select][+][-]
  1. var mls: String = "this is a"#10 +
  2.                   "multiline"#10 +
  3.                   "string";
Which works fine for most special chars (like tab #9) but of course newline is different on windows (#13#10) and unix (only #10).
I think therefore a more "pascallian" way would be to have special sequences with #, like #n for newline, which is on windows treated as #13#10 and on unix as #10.

But there is already a solution to that that works perfectly fine in current fpc:
Code: Pascal  [Select][+][-]
  1.   {$Macro on}
  2.   {$Define ln:=+ LineEnding}
  3. const
  4.   mls = 'multi'ln +
  5.         'line'ln +
  6.         'string';
If you like to also leave out the +:
Code: Pascal  [Select][+][-]
  1.   {$Macro on}
  2.   {$Define ln:=+ LineEnding +}
  3. const
  4.   mls = 'multi'ln
  5.         'line'ln
  6.         'string';
« Last Edit: December 27, 2020, 10:56:27 pm by Warfley »

speter

  • Sr. Member
  • ****
  • Posts: 338
Re: Idea of multi-line strings, mantis 35827
« Reply #12 on: December 28, 2020, 12:08:18 am »
I think it would be simpler to just use:
Code: Pascal  [Select][+][-]
  1. foo := 'something'+slinebreak+
  2.        'next line'+slinebreak+
  3.        'etc';
cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Idea of multi-line strings, mantis 35827
« Reply #13 on: December 28, 2020, 12:42:53 am »
Better load them from resources, easier to post compilation translate them
How would you do that actually?

Lots of ways, for example:
  • use a resourcestring section, though that posses the same problem with multi-line strings as any other code;
  • go down to basics and write your strings in an rc script as a string table, reading them back with a TStringTableResource, which has the advantage than some (most?) resource compilers allow you to use C-like scapes in the rc script;
  • ... etc ...

What we actually do is to use string concatenation with C-like scapes, e.g.
Code: Pascal  [Select][+][-]
  1. const {or resourcestring}
  2.   sBanner = 'I Made It! First Ever Algorithm\n' +
  3.             'Copyright 1842 by Ada A. Lovelace';
which we then pass through an "unscape' function when in use. But I realize this is just a crutch; it would be nicer if  the compiler allowed us to, at least, use another operator meaning "add a line end before concat", if not a special "multiline string" construct. Say. something like (just an example!):
Code: Pascal  [Select][+][-]
  1. const {or resourcestring}
  2.   sBanner = 'I Made It! First Ever Algorithm' *
  3.             'Copyright 1842 by Ada A. Lovelace';
instead of:
Code: Pascal  [Select][+][-]
  1. const {or resourcestring}
  2.   sBanner = 'I Made It! First Ever Algorithm'  + LineEnding +
  3.             'Copyright 1842 by Ada A. Lovelace';

And, yeah, let's not kid ourselves: the main drive for this feature is to save typing ;) but it also helps to prevent some annoying bugs, like forgetting one (or more!) of those "  + LineEnding +" or adding one where it's not wanted when you're adding a "wall-of-text" constant like, say, a console program "help". Of course, one could (like I do sometimes) just write that wall of text in its own text file and pass it through a "pascalifier", but again: less typing? lazy programmers? don't make the tool chain too long or you'll tangle in it? :D

Ok, I'm running out of ink; I'll stop here ("wall of text", indeed!) ::)


Oh! Just one thing:

But there is already a solution to that that works perfectly fine in current fpc:
Code: Pascal  [Select][+][-]
  1.   {$Macro on}
  2.   {$Define ln:=+ LineEnding}
  3. const
  4.   mls = 'multi'ln +
  5.         'line'ln +
  6.         'string';
[... etc ...]
A hint, if I may: better shut off macros or undefine "ln" when it's no longer needed; otherwise you might find all your WriteLn()'s converted to "Write+LineEnding()" ;D

ETA: In case somone takes it seriously: The compiler won't really do that; it was just to illustrate one drawback of using macros a la brava.
« Last Edit: December 28, 2020, 12:52:55 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Zoran

  • Hero Member
  • *****
  • Posts: 1824
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Idea of multi-line strings, mantis 35827
« Reply #14 on: December 28, 2020, 01:06:54 am »
Lazarus editor also provides an easy way to paste multiline string -- right click in place where you want to insert it and chose "Multi paste" (not actually a self-explaining name for that option).

For example, type this, select it and copy to clipboard:
Quote
select
  p.proj_id,
  case when p.team_leader = e.emp_no then 'project leader' else '' end as is_leader,
  e.full_name,
  p.proj_name, p.proj_desc, p.product
from project p
inner join employee_project ep on ep.proj_id = p.proj_id
inner join employee e on e.emp_no = ep.emp_no
order by 1, 2 desc

And then right-click to where you want it, chose Multi paste, and we can do this:

(see four steps in attached images)

After that, you only have to delete the first '+ LineEnding +'.

 

TinyPortal © 2005-2018