Skip to main content

Syntax Cheat Sheet

Names used in examples

ConceptAda
Reference Access
Pointer Access All
Namespace P, Q, R
Class Capricorn
Struct Scorpio
Type S, T, V, W
Variables A, B, C
Function Foo, Bar

Overview

Identifiers

Can't start with number or underscore, case insensitive

Keywords Case insensitive, usually lower case
Naming Conventions(s)

Ada_Case(types and functions), keywords

Declaration file FileName.ads
Definition file FileName.adb
Dependency
with Package.Child;
Line comment
-- line comment
Block comment N/A
Inline docs Line comment before or after element

Program Structure

Compile-time configN/A
Static assert
pragma Assert(cond);
pragma Assert(cond, message);
Namespacing
package P
Child Namespaces
package P.R
Namespacing
package P
Namespace aliasing
package TIO renames Ada.Text_IO;
Using namespace
use Ada.Text_IO;
Using subprograms of type
use type T;
Scope resolution
P.Q.R
private with Q;
limited with P;
private package P;
Ensuring module has no state
package P with Pure
No module initialization required
pragma Preelaborate(P);
Ensure elaboration immediately after specification
pragma Elaborate_Body;
Ensure other package is initialized before this one
pragma Elaborate(P);
Ensure other package and all dependencies are initialized before this one.
pragma Elaborate_All(P);
Prevent a dependency on another package
pragma Restrictions(No_Dependencies => Other_Package)

Memory

Pointer
Ptr : access all T;
Pointer to allocation from a specific pool
Ptr : access T;
Pointer deference
Ptr.all
Reference
Ptr : not null access T;
Variable used by Pointer
A: aliased T;
Address
Ptr : access T := T'Access(A)
Address
Ptr : access all T := T'Unchecked_Access(A)
Constant pointer
Ptr : constant access T;
Pointer to constant
Ptr : access constant T;
Constant pointer to constant
Ptr : constant access constant T
Prevents allocations to anonymous access types.
pragma Restrictions(No_Implicit_Heap_Allocation)
pragma Restrictions(No_Anonymous_Allocators)
Dynamic allocation
A : access T := new T;

Special Notations

Equality
A = B
Inequality
A /= B
Assignment
A := B
Array Access
A(i)
Range
min .. max
"Box"
<>
Exponentiation
Base ** Exponent
Discrete type
(<>)
"Tick"
'
String Concatenation
A & B

Expressions

qualified expression
for all A of B => expr
qualified expression
for some A of B => expr
if expression
A : Boolean := (if A then B else C);
case expression

A : Integer = (case Value is when 0 => 1, when 1 => 1, when 2 .. 4 => 5, when 5 | 9 => 10, when others => 0);

Expression renaming
L2 : Float renames V.Length * V.Length;

Mathematics

Modify in-place
A := A + 1;
Modulus
mod
Remainder
rem
Exponentiation
A ** B
Bit shifting In standard library

Control Flow

if

if A then statements; elsif B then statements; else statements; end if;

while

while A loop statements; end loop;

do-while

loop -- statements exit when A; end loop;

value-based loop

for Value in 0 .. 99 loop statements; end loop;

iterator-based loop

for Elem of Container loop statements; end loop;

Multiple choice

case Value is when 0 => Handle_Zero; when 1 => Handle_One;

when 2 .. 4 => Handle_Range; when 7 | 9 => Handle_Choices; when others => Handle_Default; end case;

Iterate over enum

for Elem in EnumName loop statements; end loop;

Stop iterating
exit
Start exception handling

declare statements; exception when A => statements; when others => statements; end;

Empty statement (pass)
null;
Label
<<LABEL_NAME>>
goto
goto LABEL_NAME

Boolean Algebra

Equality
A = B
Inequality
A /= B
Not
not A
Boolean operators
or
A or B
and
A and B
Short circuiting boolean operators
or
A or else B
and
A and then B
Exclusive-Or (XOR)
A xor B
Implies (not A, or B)
(if A then B)

Functions and Procedures

Procedure

procedure Foo(X: in T; Y: in V) is begin statements; end Foo;

Function

function Fibonacci(X: Natural) return Natural is if X = 0 or X = 1 then return X; else return Fibonacci(X - 1) + Fibonacci(X - 2); end if; end Fibonacci;

Subprogram call (no parameters)
A;
Subprogram call with named Parameters
Foo(Bar1 => Value, Baz => Value2)
Override specifier
overriding procedure Foo(obj : in Object)
Ensure that a subprogram definition does not override an existing one
not overriding procedure Foo(obj : in Object)
Pass by pointer
procedure Foo (B : in access Bar)
Pass by reference
procedure Foo(B : in Bar)
Inline
procedure Foo with Inline
Using functions for a type unqualified.
use type P.Foo;      -- Make primitive ops visible
use all type P.Foo;  -- Make all ops visible for type
Modifiable parameters
procedure Foo(B : in out Bar)
Expression function
function Foo return T is (expr)
Empty procedure
procedure Foo is null;

Types

Statically sized array
type Buffer is array(1 .. 128) of Integer;
Array Access
A(i)
Multi-dimensional Array
Mat4 : array (1 .. 4, 1 .. 4) of Float;
Built-In Variable length array
type Buffer is array(1 .. N) of Integer;
Semantic type
type Microseconds is new Integer;
Range checks on type
type My_Positive is range 1 .. 10;
Size
T'Size
Alignment
T'Alignment
Type Aliasing
subtype T is W;
Type parameterized by value (run-time)
type S(T: t) is record -- ...
Enum range
A'Range
Membership test
A in E
A not in E
Type invariant checks
type T is new V with Type_Invariant => Expr(T)
Sum types
type S is (T, V, W);
Coercion (casting)
A := B(C);
Coercion with constraint check(casting)
A := B'(C);

Object-Oriented Programming

Class-like
type T is private;
Abstract class

type T is interface; function Foo(A : T) return V is abstract; procedure Bar(A: in out T) is abstract;

Subprogram call of object-like type
A.B;
Subprogram call of object-like type (alternative function-style form)
B (A);
Member access from pointer
A.all.B; -- Explicit
A.B; -- Implicit
Prevent copying
type X is limited type;
Inheritance
type Foo is Bar with null record;
Dynamic dispatch (virtual function call)
procedure Foo(A : T'Class)
Prevent implicit cast
type T is new W;
Runtime type checking
if A in T then end if;
Passing parameter by base class
procedure Foo(A : BaseClass'Class)
Array-like indexing of user-defined type.

type My_Container is tagged type with Constant_Indexing =>Foo Variable_Indexing => Bar -- Foo and Bar are functions defined on the type.

Automatic dereference of a handle-type to the handle's contents.

type Handle(Target: not null access Element) is with Implicit_Dereference => Element;

-- Old usage, calling Foo A_Handle.Target.all.Foo -- New usage A_Handle.Foo

Iterator for loops for user-defined types.

type My_Container with Default_Iterator => Iterate, Iterator_Element => Element_Type;

type Cursor;

function First (M : in My_Container) return Cursor; procedure Next (C : in out Cursor); function Has_Element (C : in Cursor) return Boolean;