TamaMcGlinn
TamaMcGlinn

Reputation: 3248

Multiline string literal in Ada

How can I create a string in Ada containing newlines, whose definition also has those newlines?

I've tried with 0..2 backslashes at the end of the line, but none of that compiles:

   usage_info : String := "\
This should be the first line
and both the definition and the output
should contain newlines.";

In PHP this would be:

<<<BLOCK
1
2
3
BLOCK;

In C++ this would be:

const std::string s = "\
1
2
3";

In C#, it would be:

const string s =
@"1
2
3";

Upvotes: 2

Views: 2306

Answers (2)

LoneWanderer
LoneWanderer

Reputation: 3341

A complement to Frédéric Praca answer:

Depending on your needs, you can use ASCII package instead of Ada.Characters.* (such as Latin_1, Latin_9, Wide_Latin_.. etc.). ASCII can not be with'ed since it is not a package, so you'll have to prefix everything (or define "aliases" using renames)

declare
    flex : constant String := "Foo" & ASCII.CR & "bar" & ASCII.LF;
    flux : constant String := "Foo" & ASCII.CR
                            & "bar" & ASCII.LF;
begin
    -- do stuff
    null;
end;

One could define a custom & operator to use it as a new line insertion point. But ... how useful is it ?

function Foo (Left, Right : String) return String renames "&";
function Boo (Left : String; Right : Character) return String renames "&";

function "&" (Left, Right : String) return String is begin
   return Foo (
               Boo (Left, ASCII.LF),
               Right);
end "&";

Ada.Text_IO.Put_Line("Foo" &
                     "bar");

Upvotes: 2

Fr&#233;d&#233;ric Praca
Fr&#233;d&#233;ric Praca

Reputation: 1641

From what I know, Ada , like Java, does not support multiline literal. The only thing I see is something like this:

usage_info : String := "This should be the first line" & CR & LF
                     & "and both the definition and the output" & CR & LF 
                     & "should contain newlines.";

Of course, you need to with and use Ada.Characters.Latin_1 to make these constants visible.

Upvotes: 8

Related Questions