Exit in Ada

Here’s the translation of the Go “Exit” example to Ada, formatted in Markdown suitable for Hugo:

Our program demonstrates how to exit immediately with a given status.

with Ada.Text_IO;
with Ada.Command_Line;

procedure Exit_Example is
begin
   -- This Ada.Text_IO.Put_Line will not be executed
   -- as we exit before reaching it
   Ada.Text_IO.Put_Line("!");

   -- Exit with status 3
   Ada.Command_Line.Set_Exit_Status(3);
   Ada.Command_Line.Set_Exit_Status_Last_Chance(3);
end Exit_Example;

In Ada, we use Ada.Command_Line.Set_Exit_Status to set the exit status. However, unlike some other languages, Ada will still execute all finalization code before exiting. To ensure immediate exit without finalization, we use Set_Exit_Status_Last_Chance.

Note that unlike some other languages, Ada does not use an integer return value from the main procedure to indicate exit status. If you’d like to exit with a non-zero status, you should use Ada.Command_Line.Set_Exit_Status or Set_Exit_Status_Last_Chance.

If you compile and run this Ada program, the exit status will be set to 3:

$ gnatmake exit_example.adb
$ ./exit_example
$ echo $?
3

Note that the ! from our program never got printed, as we exited before reaching that line.

In Ada, unlike some other languages, there’s no direct equivalent to deferred execution that’s cancelled by early exit. The closest concept would be exception handlers or controlled types for resource management, but these will still execute during normal program termination.

This example demonstrates how to exit a program with a specific status code in Ada, which can be useful for communicating the program’s state to the calling environment.