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.
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:
'x'
Foo'Size
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.)
You seem to be asking specifically about qualified expressions (Keith's third bullet in his answer).
In Ada, if you have two objects of different types you can attempt to convert between them by using the destination type's name like a function name, like so:
Foo : constant Integer := Integer (2.35);
Generally this only works if both types are numeric types, or if one is derived from the other (declared as type New_Type is new Old_Type
...).
The compiler will of course have to add code to verify that the value falls within any contraints the destination type may have. But this is very useful for simple type conversions.
However, when you are dealing with expressions, sometimes what you want isn't a conversion, but rather to tell the compiler what type to make the expression. No (runtime) code should be required to do this, just make the expression the type I tell you to.
Compilers can generally figure this out from context, but sometimes they can't. This is where that apostrophe comes in. It tells the compiler not to convert the expression to the specified type, but rather to create it as that type in the first place.
The most common use for this is when performing dynamic allocations, as shown in your example. Sometimes there may be other situations where it is needed though. One example might be when passing a literal value into an overloaded routine. Say you have two versions of the procedure My_Routine
, one that takes in an Integer
, and the other taking in a different custom integer type. If you pass objects into it, the compiler can just look at the object's type. However, if you pass in a literal 1
, most likely you will get a compiler error that the expression is ambiguous.
You could solve this by putting your literal 1
into a constant integer and passing that in (then grumbling about your stupid compiiler). However, the easier thing to do is the following:
My_Routine (Integer'(1));
That resolves the ambiguity for your compiler. This isn't a "conversion", so no extra code is needed. You are just telling the compiler that the following expression is of type Integer
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With