Overview of Ada 2022
3.6 Declare expressions
Declare expressions (AI12-0236)
(see RM
4.5.9).
As the power of expressions has grown, some felt that it would help to
allow local constants and object renamings within an expression, to avoid
repeated subexpressions.
For example, the postcondition
for
Fgetc could be clarified from:
(if Stream.The_File (Stream.Cur_Position'Old) =
EOF_Ch
then Stream.Cur_Position = Stream.Cur_Position'Old
and then Result = EOF
elsif Stream.The_File (Stream.Cur_Position'Old) =
ASCII.LF
then Stream.Cur_Position = Stream.Cur_Position'Old
and then Result = Character'Pos (ASCII.LF)
else
Stream.Cur_Position = Stream.Cur_Position'Old + 1
and then Result = Character'Pos (Stream.The_File
(Stream.Cur_Position'Old)))
to:
(declare
Old_Pos : constant Position :=
Stream.Cur_Position'Old;
The_Char : constant Character :=
Stream.The_File(Old_Pos);
Pos_Unchg : constant Boolean :=
Stream.Cur_Position = Old_Pos;
begin
(if The_Char = EOF_Ch
then Pos_Unchg and then Result = EOF
elsif The_Char = ASCII.LF
then Pos_Unchg and then Result =
Character'Pos(ASCII.LF)
else
Stream.Cur_Position = Old_Pos + 1
and then Result = Character'Pos (The_Char)))
This uses the reserved words
declare and
begin,
as for a block, but not
end, as it is only used within parentheses.
Declare expressions can be static (AI12-0368)
allows these new declare expressions to be static.
© 2021, 2022 Jeff Cousins