with Ada.Text_IO; use Ada.Text_IO; procedure hello is begin put_line("Hello there!"); end hello;
>gnatmake hello ... Compiler output omitted >./hello Hello there! >
n: Natural; small: Integer range 1 .. 10; ... small := n; -- Will an error occur?
subtype Negative is Integer range Integer'first .. -1;
v: Negative;
n: Natural;
...
v := n; -- Compiler warning, runtime error;
v
can hold only negative numbers double penny = 0.01; double sum = 0; for (int i = 0; i < 100; i++) sum += penny; if (sum == 1.00) System.out.println("A dollar"); else System.out.println("Not a dollar");
type Dollars is delta 0.01 range 0.00 .. 1_000_000.00
penny: constant Dollars := 0.01;
sum: Dollars := 0.00;
for i in 1 .. 100 loop
sum := sum + penny;
end loop;
if sum = 1.00 then put_line("A dollar");
else put_line("Not a dollar");
end if;
... subtype Even is Integer with Dynamic_Predicate => Even mod 2 = 0; e: Even; n: Natural; ... e := n; -- Catch error on assignment, not use e := e + 1; -- Catch on compilation?
e
can only hold Even numbers... subtype Even is Integer with Dynamic_Predicate => Even mod 2 = 9; e: Even; n: Natural; ... e := n; -- Catch error on assignment, not use e := e + 1; -- Catch on compilation?
-- A bit is, well, size one bit
type Bit is Integer range 0 .. 1
with size => 1;
-- A Bit_Array is, well, size 32 bits
type Bit_Array is array (1 .. 32) of Bit
with size => 32;
b: Bit_Array;
-- Records with fields
type Sensor is record
Active: Boolean;
temperature: Integer range 0 .. 127
end record;
-- Specify size
for Sensor'Size use 8; -- 8 bits
-- Specify bit layout
for Sensor use record
Active at 0 range 0 .. 0; -- first bit
temperature at 0 range 1 .. 7; -- remaining 7 bits
end record;
s: Sensor;
... -- Check if needle is in hay at position loc function match (needle, hay: String; loc: Positive) return Boolean is (for all i in 1 .. needle'length => needle(i) = hay(loc + i - 1)) with pre => loc in hay'length - needle'length - 1; -- Find location of needle in hay, if it's there function search (needle, hay: String) return Natural with pre => needle'length in 1 .. hay'length, post => (search'result in 0 .. hay'length - needle'length + 1 and then (if search'result > 0 then match(needle, hay, search'result) else (for all loc in 1 .. hay'length - needle'length + 1 => not match(needle, hay, loc)) ));
4 versions: 1983, 1995, 2005, 2012 (very backwards compatible)
Ada 83
Ada 95: OO added - First international standard OO language