Skip to content
Adam
Start

Tour

A first program, how to build and run it on both targets, and one short stop at each part of the language. Every example here is built and run by the docs' check.

A first program

A program is a module headed module exe <name>;; the name is the file's name. Save this as hello.adam:

hello.adam
module exe hello;

begin
  println("Hello from Adam!");
end.

Build it and run it:

text
adam build hello.adam -r
output on x86_64-windows-gnu and x86_64-linux-gnu
Hello from Adam!

adam build writes the exe into the bin folder of the output folder: output under the folder you ran adam in, unless -o, the module's @outputpath or its project names another. A debug build, the default, also prints a heap report when the program ends; a release build such as -opt fast prints only the program's own output, as every example on these pages does. More on Building and The adam command; a folder of sources with a project.toml is a project (Projects).

Both targets

The same source builds for x86_64-linux-gnu from Windows, and with -r the Linux program runs through WSL. -opt picks the optimization level: debug, safe, fast or small.

text
adam build hello.adam -t x86_64-linux-gnu -r
adam build hello.adam -opt fast -r

A build defines TARGET_<triple> for its own target, so one source can hold code for each (Targets):

directives_target.adam
module exe directives_target;

begin
  // The branch for the build's own target is compiled; the other is left out
@ifdef TARGET_X86_64_WINDOWS_GNU
  println("Built for x86_64-windows-gnu");
@else
  println("Built for x86_64-linux-gnu");
@endif
end.
output on x86_64-windows-gnu
Built for x86_64-windows-gnu

Declarations

A constant always names its type, a module-level variable starts at zero, a const parameter is read only and a var parameter is the caller's own variable.

declarations_basics.adam
module exe declarations_basics;

const
  LIMIT: int64 = 3;

var
  GTotal: int64;

// AValue doubled
func Twice(const AValue: int64): int64;
begin
  return AValue * 2;
end;

// The caller's own variable, grown by one
func Bump(var AValue: int64);
begin
  AValue := AValue + 1;
end;

begin
  println("starts at {}", GTotal);
  Bump(GTotal);
  GTotal := Twice(GTotal) + LIMIT;
  println("now {}", GTotal);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
starts at 0
now 5

Types

A choices value is written with its type, a record is built by naming its fields, and array of T grows with setlength.

types_records.adam
module exe types_records;

type
  Suit = choices(Hearts, Spades);

  Card = record
    Rank: int32;
    Kind: Suit;
  end;

// The card in words
func Describe(const ACard: Card): string;
var
  LSuit: string;
begin
  match ACard.Kind of
    Suit.Hearts:
      LSuit := "hearts";
    Suit.Spades:
      LSuit := "spades";
  end;
  return FormatStr("{} of {}", ACard.Rank, LSuit);
end;

var
  GHand: array of Card;
  GIndex: int64;

begin
  setlength(GHand, 2);
  GHand[0] := Card(Rank: 7, Kind: Suit.Hearts);
  GHand[1] := Card(Rank: 12, Kind: Suit.Spades);
  for GIndex := 0 to len(GHand) - 1 do
    println("{}", Describe(GHand[GIndex]));
  end;
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
7 of hearts
12 of spades

Objects

An object carries its methods and reaches itself through self. A method is virtual only when it says so, a kind that replaces it says override, and a ref to a derived object goes wherever a ref to its base is asked for.

objects_virtual.adam
module exe objects_virtual;

type
  Shape = object
    Title: string;

    virtual method Area(): int64;
    begin
      return 0;
    end;

    // The call through self reaches the object's own kind
    method Describe();
    begin
      println("{} has area {}", self.Title, self.Area());
    end;
  end;

  Square = object(Shape)
    Side: int64;

    override method Area(): int64;
    begin
      return self.Side * self.Side;
    end;
  end;

  Rectangle = object(Shape)
    Width: int64;
    Height: int64;

    override method Area(): int64;
    begin
      return self.Width * self.Height;
    end;
  end;

// Any shape, reached through a ref to the base
func Show(const AShape: ref Shape);
begin
  AShape.Describe();
end;

var
  GSquare: ref Square;
  GRect: ref Rectangle;

begin
  GSquare := ref Square;
  GSquare.Title := "square";
  GSquare.Side := 3;
  GRect := ref Rectangle;
  GRect.Title := "rectangle";
  GRect.Width := 4;
  GRect.Height := 5;
  Show(GSquare);
  Show(GRect);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
square has area 9
rectangle has area 20

Sequences

A routine whose result is a sequence of T hands out its values one yield at a time, and for .. in takes them as they come.

statements_sequences.adam
module exe statements_sequences;

// The first ACount squares, made one at a time
func Squares(const ACount: int32): sequence of int32;
var
  LI: int32;
begin
  for LI := 1 to ACount do
    yield LI * LI;
  end;
end;

var
  GValue: int32;

begin
  for GValue in Squares(5) do
    println("{}", GValue);
  end;
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
1
4
9
16
25

Exceptions

raise leaves a guard's body for its except, excmsg() gives the message, and finally runs on the way out.

statements_guard.adam
module exe statements_guard;

// AValue, when it is not negative
func Checked(const AValue: int32): int32;
begin
  if AValue < 0 then
    raise("negative value");
  end;
  return AValue;
end;

begin
  guard
    println("{}", Checked(4));
    println("{}", Checked(-1));
    println("not reached");
  except
    println("caught: {}", excmsg());
  finally
    println("finally runs");
  end;
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
4
caught: negative value
finally runs

Concurrency

async starts a call on a thread of its own and gives a task; await waits for it and gives the result.

concurrency_async.adam
module exe concurrency_async;

// 1 + 2 + .. + ACount
func Sum(const ACount: int64): int64;
var
  LI: int64;
  LTotal: int64;
begin
  LTotal := 0;
  for LI := 1 to ACount do
    LTotal := LTotal + LI;
  end;
  return LTotal;
end;

var
  GSmall: task int64;
  GLarge: task int64;

begin
  GSmall := async Sum(10);
  GLarge := async Sum(1000);
  println("{} and {}", await GSmall, await GLarge);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
55 and 500500

C functions

Declare a C function with C linkage and an external clause, and call it like any routine:

external_c.adam
module exe external_c;

// C's abs, declared with C linkage
func clink abs(const AValue: int32): int32; external "c";

begin
  println("{}", abs(-42));
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
42

Another name for a symbol, callbacks and variadics are on External declarations; a whole library's binding is written by adam cimport.

Errors

A mistake is reported at the .adam file, line and column, and an error stops the build. Here an int64 is stored into an int32 with no cast:

errorSM0200…/samples/diagnostics_narrowing.adam(33,13)
'int64' does not convert implicitly to 'int32'; write int32(...)

How reports read is on Diagnostics; every code the compiler reports is listed in the Diagnostics.

Tests

Test blocks follow a module's end.. With @unitTestMode on; the exe runs them instead of its body, and its exit code is the number of tests that failed.

testing_runner.adam
module exe testing_runner;

@unitTestMode on;

// AValue squared
func Square(const AValue: int64): int64;
begin
  return AValue * AValue;
end;

begin
  println("the body does not run in test mode");
end.

test "squares a positive number"
begin
  testAssertEqualInt(9, Square(3));
end;

test "squares a negative number"
begin
  testAssertEqualInt(16, Square(-4));
end;
output on x86_64-windows-gnu and x86_64-linux-gnu

╔══════════════════════════════════════════════════════════════╗
║                     Unit Test Runner                         ║
╚══════════════════════════════════════════════════════════════╝

Running 2 test(s)...

✅ PASS: squares a positive number
✅ PASS: squares a negative number

════════════════════════════════════════════════════════════════
Results: 2 passed, 0 failed, 2 total
════════════════════════════════════════════════════════════════

Where next

↑ ↓ moveEnter openEsc close