Multi-line String Literals Planned For Delphi 12 Yukon🤞

This blog post is based on a pre-release version of the RAD Studio software and it has been written with specific permission by Embarcadero. No feature is committed until the product GA release.

The upcoming new release of RAD Studio 12 Yukon is a major update that's got a new Delphi language features that I've been wanting for years, or perhaps even decades since I first discovered heredocs in PHP.

Yep, we're going to get multi-line string literals. This excites me just as much as the earlier introduction of inline var statements, which was a lot!

I write a lot of multiline string constants in my code like this:

const
  S0 = '
Lorem ipsum dolor sit amet, consectetur adipiscing elit.' + 
       sLineBreak  +
       '  Vestibulum eleifend elit id dapibus pulvinar.' +
       sLineBreak +
       'Suspendisse tincidunt, diam vel dapibus aliquam.';

Miss out one of the closing quotes or + signs and bang, compile failure. Annoying.

But now we have multi-line string literals, delimited by triple single quotes ('''), I can just write this:

const
  S1 = '''
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Vestibulum eleifend elit id dapibus pulvinar.
  Suspendisse tincidunt, diam vel dapibus aliquam.
  ''';

  S2 = '''
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      Vestibulum eleifend elit id dapibus pulvinar.
    Suspendisse tincidunt, diam vel dapibus aliquam.
    ''';

I'm more happy about this than I have any right to be. 😁

There's been a lot of discussion about whether text should be able to be on the same line as the ''' symbols and how indenting should work. In the end the rules seem to be:

  1. The opening ''' must not have any text after it on the same line. The following new line is ignored.
  2. There must be no text preceding the closing '''. The preceding new line is ignored.
  3. The indentation used for the closing ''' determines the start position of the text. Text with the same indentation as the closing ''' has no preceding spaces. Any indentation beyond that of the closing ''' causes a number of spaces equal to the difference in indentation to be used. Indenting text to a lesser amount that the closing ''' is a compile error.
So, S0, S1 and S2 above all render the same, like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  Vestibulum eleifend elit id dapibus pulvinar.
Suspendisse tincidunt, diam vel dapibus aliquam.

If you want to include triple single quotes in the text itself you can do this by replacing the enclosing triple quotes by any larger odd number of single quotes that don't occur in the text, say 5 or 7 of them:

var V := '''''
  Have you hear about the new ''' symbol
  due for Delphi Yukon?
  ''''';

The types of new lines used, and more, can be configured using the new TEXTBLOCK compiler directive. I haven't got my head round this yet, so I'm not going to even try to explain it here! 😶

Comments

Popular posts from this blog

Deleting elements from a dynamic array

Initialising dynamic arrays

New String Property Editor Planned For RAD Studio 12 Yukon 🤞