Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Ada Type Ranges

Demonstrates Ada's strong type system with range constraints - compile-time and runtime safety for numeric values with explicit valid ranges.

The Problem

In most programming languages, numeric types accept any value within their storage capacity:

int age = 200;        // Compiles fine, but humans don't live to 200
int floor = -100;     // Compiles fine, but buildings don't have 100 basement levels
int httpStatus = 999; // Compiles fine, but not a valid HTTP status

These invalid values slip through and cause bugs that only manifest at runtime - or worse, silently corrupt data.

The Solution

Ada allows defining types with explicit value ranges:

type Age_Type is range 0 .. 150;
type Floor_Type is range -5 .. 100;
type HTTP_Status is range 100 .. 599;

Key Benefits

Compile-Time: Exhaustive Case Statements

Case statements must cover ALL possible values. Missing cases result in a compile error:

type Age_Type is range 0 .. 150;

case Age is
   when 0 .. 17 => Put_Line("Minor");
   when 18 .. 64 => Put_Line("Adult");
   -- COMPILE ERROR: missing case value(s) 65 .. 150
end case;

Runtime: Constraint Violations

Assigning out-of-range values raises Constraint_Error:

Age : Age_Type := 200;  -- Raises Constraint_Error at runtime

Subtypes: Named Subranges

Define meaningful subranges for cleaner code:

type HTTP_Status is range 100 .. 599;
subtype Success is HTTP_Status range 200 .. 299;
subtype Client_Error is HTTP_Status range 400 .. 499;
subtype Server_Error is HTTP_Status range 500 .. 599;

Files

File Description
type_ranges_demo.adb Main demo showing Age, Floor, and HTTP Status examples
incomplete_case_demo.adb Intentionally fails to compile - demonstrates exhaustiveness checking

How to Run

Using Docker (Recommended)

# Build the image
docker build -t ada-type-ranges .

# Run all demos
docker run --rm ada-type-ranges

# Run only the main demo
docker run --rm ada-type-ranges sh -c "gnatmake -q type_ranges_demo.adb && ./type_ranges_demo"

# See the compile error demo
docker run --rm ada-type-ranges gnatmake incomplete_case_demo.adb

Local Installation

Requires GNAT (GNU Ada Compiler):

# Debian/Ubuntu
apt-get install gnat

# macOS with Homebrew
brew install gnat

# Compile and run
gnatmake type_ranges_demo.adb
./type_ranges_demo

# See the compile error
gnatmake incomplete_case_demo.adb

Expected Output

Main Demo

=== Domain 1: Age (0..150) ===
Age 0:   Newborn
Age 2:   Toddler
Age 10:  Child
Age 16:  Teenager
Age 35:  Adult
Age 75:  Senior

=== Domain 2: Building Floors (-5..100) ===
Floor -3: Underground Parking Level -3
Floor 0:  Ground Floor / Lobby
Floor 5:  Lower Floors (Commercial)
Floor 50: Residential Floors
Floor 95: Penthouse Level

=== Domain 3: HTTP Status Codes (100..599) ===
Status 200: 2xx Success
Status 404: 4xx Client Error
Status 500: 5xx Server Error

=== Runtime Constraint Violation ===
Attempting to assign 200 to Age_Type (range 0..150)...
CAUGHT: Constraint_Error!
   Ada prevented an invalid value at runtime.

Compile Error Demo

incomplete_case_demo.adb:10:07: error: missing case value(s) 65 .. 150
gnatmake: "incomplete_case_demo.adb" compilation error
The compiler caught the missing case at compile time!

References