Currently, AL only supports single-line string literals, which makes working with multiline-strings a bit cumbersome.
// works:
myText := 'Hello';
// error:
myText := '
Hello
';
So, if you want to have long strings or strings like JSON/XML span multiple lines your only option is concatenation:
// before:
myText := '{ "foo": 1, "bar": 2 }';
// after:
myText := '{' +
'"foo": 1,' +
'"bar": 2' +
'}';
// Note, you also have to concat Newline endings and indentation/tab characters,
// (if you want the string to also include those)
NL[1] := 13;
NL[2] := 10;
TB[1] := 9;
myText := '' + NL +
TB + '{' + NL +
TB + TB + '"foo": 1,' + NL +
TB + TB + '"bar": 2' + NL +
TB + '}' + NL +
'';
// Imagine if our json has more properties :)
Other programming languages tend to have a multline string support, so could AL add something similar?
// Javascript/Typescript:
myText := `
{
"foo": 1,
"bar": 2
}
`;
// Dotnet (personally, I don't like the quotation escapes though):
myText := @"
{
""foo"": 1,
""bar"": 2
}
";
// Python uses triple-quotes (single or double) for opening/closing
// PHP uses double vs. single quotes
// C++ 11 supports raw string e.g. in the format: R""""(YourStringGoesHere)"""
// Free-Pascal doesn't have it yet I believe but users seem to generally favor backticks
And just as a bonus, I think it would be a great addition to throw interpolation in there as well:
// before:
myText := StrSubstNo(`
{
"foo": %1,
"bar": %2
}
`, GetRandomNumber(), GetRandomNumber());
// after:
myText := `
{
"foo": ${GetRandomNumber()},
"bar": ${GetRandomNumber()}
}
`;
Making string literals more rich will make integrations more straight-forward, e.g. when doing JsonObject.ReadFrom where you already have the string you want to place in code, instead of using JsonObject.Add, it would make generation of files/requests easier, etc.
It would also just make these cases much more readable (and easier to share snippets between languages/platforms).
Comments
Also very helpfull in ToolTips, because the are tending to be very long...
Category: Development
I really like this idea! Generally if I have needed a long string literal, I've used a controladdin and a resource to load it into the page. Just to keep it out of the code.If you really need it in the code, I would recommend using a TextBuilder rather than concatenation.
Category: Development
Business Central Team (administrator)
Thank you for this suggestion! Currently this is not on our roadmap. We are tracking this idea and if it gathers more votes and comments we will consider it in the future. Best regards, Business Central Team