Search code examples
new-operatorrecordadaargumentsquote

Anyone can give me a summary of "single quote mark" usage in Ada?


I've just read "Ada Programming" but I'm a bit confused about how to use ' (single quote mark) in Ada.

I can understand that ' is used for reference attribute. AAA'Image(..), BBB'Value(..)

However, considering this piece of code:

   type Plain_Vector (Capacity : Capacity_Subtype) is record
      Elements : Elements_Array (1 .. Capacity);
      Last     : Extended_Index := No_Index;
      Busy     : Natural := 0;
      Lock     : Natural := 0;
   end record;
 ------------------------------------------------------------------
   new Plain_Vector'(2, (Left, Right), Last => Last, others => <>)

Q1: How the "new" statement's arguments matches the type's parameter and record fields?

 I can GUESS "2" matched "Capacity", 
             "(Left, Right)" matched "Elements", 
             "Last => Last" matched "Last"
             "Others => <>" matched "Busy" and "Lock" to let them use default value.

But this is just a GUESS, are there any official grammar explanation on this?

Q2: What does the ' do? (in the "new" statement)

Is it an attribute or does it have any other meanings?

Where can I find a summary usage of "single quote mark" in Ada?

I spent long time trying to find out those information, but no luck.

Thank you in advance. Miles.


Solution

  • If you have a soft copy of the Ada Reference Manual, you can search for the ' character in the Syntax Summary (it's Annex P in the latest version I have; check the table of contents).

    The ' character is used for:

    • Character literals: 'x'
    • Attribute references: Foo'Size
    • Qualified expressions: Some_Type'(expression), Some_Type'Aggregate

    It's also used in representation clauses (now called "aspect clauses"); these look a lot like attribute references: for Foo'Size use 32;.

    And of course it can appear in a comment or in a string or character literal.

    The example in the code you posted is a qualified expression.

    Suggestion: In contexts other than character literals the character ' should probably be referred to as an apostrophe, since it's not acting as a quotation mark. For attributes and qualified expressions, it's sometimes pronounced "tick": I'd read Foo'Size as "foo tick size".

    (And new is an expression, not a statement.)