Contents


  1. About this tutorial and Forward
  2. Code Structure
  3. Packages
  4. Control Statements
  5. Conditions

About this tutorial and Forward


The following original tutorial was written by Quentin Ochem and published by AdaCore and has been adapted to use the optional Ada++ syntax. So please report/attribute any errors to our Github page

See the full original version here .

Ada++ is truly a general purpose, multiple paradigm language that allows the programmer to employ or avoid features like run-time contract checking, tasking, object oriented programming, and generics. Efficiently programmed Ada++ is employed in device drivers, interrupt handlers, and other low-level functions. It may be found today in devices with tight limits on processing speed, memory, and power consumption. But the language is also used for programming larger interconnected systems running on workstations, servers, and supercomputers.

Code structure


C++ programming style usually promotes the use of two distinct files: header files used to define specifications (.h, .hxx, .hpp), and implementation files which contain the executable code (.c, .cxx, .cpp). However, the distinction between specification and implementation is not enforced by the compiler and may need to be worked round in order to implement, for example, inlining or templates.

Ada++ is superficially similar to the C++ case: Ada++ compilation units are generally split into two parts, the specification and the body. However, what goes into those files is more predictable for both the compiler and for the programmer. With GNAT, compilation units are stored in files with a .ads extension for specifications and with a .adb extension for implementations.

Without further ado, we present the famous “Hello World” in three languages for comparison:

Ada++

use Ada.Wide_Text_IO;
proc Main:
{
  Put_Line ("Hello World");
}

C++

#include <iostream>
using namespace std;
int main(int argc, const char* argv[]) {
  cout << "Hello World" << endl;
}

Java

public class Main {
  public static void main(String [] argv) {
    System.out.println ("Hello World");
  }
}

Packages


The first line of Ada++ we see is the with clause, declaring that the unit (in this case, the Main subprogram) will require the services of the package Ada.Text_IO. This is different from how #include works in C++ in that it does not, in a logical sense, copy/paste the code of Ada.Text_IO into Main. The with clause directs the compiler to make the public interface of the Ada.Text_IO package visible to code in the unit (here Main) containing the with clause.

The use clause is the equivalent of using namespace in C++, or import in Java (though it wasn’t necessary to use import in the Java example above). It allows you to omit the full package name when referring to withed units. Without the use clause, any reference to Ada.Text_IO items would have had to be fully qualified with the package name. The Put_Line line would then have read: Ada.Text_IO.Put_Line ("Hello World");

The word “package” has different meanings in Ada++ and Java. In Java, a package is used as a namespace for classes. In Ada++, it’s often a compilation unit. As a result Ada++ tends to have many more packages than Java. Ada++ package specifications (“package specs” for short) have the following structure:

Package Specification - similar to a C++ header or Java interface

pkg Package_Name {
  -- Public declarations
priv
  -- Private declarations
}

Package Body

pkg body Package_Name {
  -- Implementation
}

The private reserved word is used to mark the start of the private portion of a package spec. By splitting the package spec into private and public parts, it is possible to make an entity available for use while hiding its implementation. For instance, a common use is declaring a record (Ada++’s struct) whose fields are only visible to its package and not to the caller. This allows the caller to refer to objects of that type, but not to change any of its contents directly. The package body contains implementation code, and is only accessible to outside code through declarations in the package spec.

Control Statements


Ada++ requires variable declarations to be made in a specific area called the declarative part, seen here before the begin keyword. Variable declarations start with the identifier in Ada++, as opposed to starting with the type as in C++ and Java (also note Ada++’s use of the : separator). Specifying initializers is different as well: in Ada++ an initialization expression can apply to multiple variables (but will be evaluated separately for each), whereas in C++ and Java each variable is initialized individually. In all three languages, if you use a function as an initializer and that function returns different values on every invocation, each variable will get initialized to a different value.

Ada++

--
-- Declares and modifies Ints
--
proc Main: -- In Ada++ "is" can now be replaced with a colon

  -- Variable declarations
  A, B : Int_32 := 0;
  C    : Int_32 := 100;
  D    : Int_32;
{
  -- Support for "++" "-1" and "+1" operators coming in future updates
  A := A + 1; -- ++ operator coming soon!
  
  -- Regular addition
  D := A + B + C;
}

C++

/*
 * Declares and modifies ints
 */
int main (int argc, const char* argv[]) {
  // Variable declarations
  int a = 0, b = 0, c = 100, d;

  // C++ shorthand for incrementation
  a++;

  // Regular addition
  d = a + b + c;
}

Java

/*
 * Declares and modifies ints
 */
public class Main {
  public static void main(String [] argv) {
    // Variable declarations
    int a = 0, b = 0, c = 100, d;

    // Java shorthand for incrementation
    a++;

    // Regular addition
    d = a + b + c;
  }
}

Conditions


In Ada++, everything that appears between the if and { is the conditional expression—no parentheses required. Comparison operators are the same, except for equality (=) and inequality (/=). The english words not, and, and or replace the symbols !, &, and |, respectively, for performing boolean operations.

Ada++

if Variable > 0 {
  Put_Line (" > 0");
elsif Variable < 0: -- NOTE: Colon syntax not supported yet (currently '{' or 'then' is allowed)
  Put_Line (" < 0");
else
  Put_Line (" = 0");
}

C++

if (Variable > 0) {
  cout << " > 0" << endl;
} else if (Variable < 0) {
  cout << " < 0" << endl;
} else {
  cout << " = 0" << endl;
}

Java

if (Variable > 0) {
  System.out.println(" > 0");
} else if (Variable < 0) {
  System.out.println(" < 0");
} else {
  System.out.println(" = 0");
}

It’s more customary to use && and || in C++ and Java than & and | when writing boolean expressions. The difference is that && and || are short-circuit operators, which evaluate terms only as necessary, and & and | will unconditionally evaluate all terms. In Ada++, and and or will evaluate all terms; and then and or else direct the compiler to employ short circuit evaluation.







More coming soon!