Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

[Yukon Beta Blog] Delphi Language: Modernizing String Literals

rad12betastringliterals

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.

As with most other programming languages, Delphi’s Object Pascal allows a developer to use string literals, that is a string value written directly in the code. This is very common, even if not always recommended; it is better to use a  resource string since they offer advantages in terms of memory management and translation support for string to show to the user. 

Now string literals in Delphi have been tied since the early days to the original Turbo Pascal string type, which is the type we refer to today as “short strings”. Short strings have a maximum of 255 characters and are allocated directly as a static array. Delphi introduced dynamically allocated, long strings starting with Delphi 2. Initially long strings were based on Ansi, and later on Unicode. However, string literals never made the same transition from the early days, and they have been limited to the short string type, and retained  a maximum of 255 characters.

The 12.0 version of Delphi (and RAD Studio) introduces a very significant set of changes to the way the Delphi compiler handles string literals.

Long String Literals

With the coming release, it will be possible to have string literals longer than 255 characters. There is no specific limitation in the compiler. However, the length of a literal string might still be constrained by the editor’s limitations of 4K characters per line. There is no change in syntax; you can simply have a literal string longer than 255 chars.

Above: A 600+ characters long string literal in the editor.

Multiline Strings

What’s more relevant is the second extension to string literals is Delphi 12 will add support for multiline strings. In this case, you have to adopt a different syntax:  multiline strings are delimited by a triple quote (’’’).

There are a few more syntax rules for a multiline string :

What’s significant, compared to the past, is that there is no need to concatenate the lines into single-line strings with a + sign. Here is an example:

Above: A multiline string literal in the editor.

Update: What’s the formatting of the string above? Bonus feature: TEXTBLOCK directive

Following questions after publishing the blog post, let me clarify the strJSON string content. The initial and file new lines are part of the syntax and not included. There are newlines for each line. For those you can use a new directive defined as:

{$TEXTBLOCK NATIVE/CR/LF/CRLF [<ident>]}

In this directive you can specify how the line breaks in the multi line strings are treated. The additional ident parameter is a decoration, ignored by the compiler but used by external tools. It may be HTML, XML, JSON, SQL, etc to indicate the content and possibly drive a syntax highlighting tool.

Last element is the indentation. As I wrote the closing triple quote position determines the “ignored indentation”. So in this case there will be two spaces for each of the three indented lines and no indentation for the first and the last line.

Further update: What about having a triple quote in a multiline string?

Notice that the triple quotes (”’) can also be replaced with a large odd number of quotes, like 5 or 7. This allows embedding an actual triple quote within a multiline string. Example:
[crayon-676d7aa0d6136745368902/]

Want to try now?

These features are enabled in the current Yukon beta, available to customers with an active update subscription. If you are in that group and haven’t received an invite, you can contact an Embarcadero sales rep or sales partner and ask for the signup link.

If you don’t have an active subscription, you can consider buying a new license or contacting sales, and once you have a license you can ask  to join the beta.

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.

Exit mobile version