Skip to content
Adam
Language

Types

The builtin types, choices, records, sets, arrays and pointers.

Examples: res\examples\language\types holds small, commented programs for this page's features, each listing the output it prints; run one with adam build <file> -r.

Builtin types

int8, int16, int32 and int64; uint8 .. uint64; clong and culong, C's long, 4 bytes on x86_64-windows-gnu and 8 on x86_64-linux-gnu; float32 and float64; bool, char and wchar; string and wstring; ptr; and mutex, a lock for threads (Locks).

Choices, records and arrays

A choices type lists its values, and a value is always written with its type (Suit.Hearts). A record value is built from its type's name and its fields by name. array of T is a dynamic array: setlength sets its length and len gives it.

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

A record is plain data that starts zeroed. record(Base) extends Base, its fields first, and an extension converts to its base: by value, the base part copied, and to a const or var parameter of the base type.

types_extension.adam
module exe types_extension;

type
  Point = record
    X: int32;
    Y: int32;
  end;

  // Point's fields come first, then its own
  Point3 = record(Point)
    Z: int32;
  end;

// Any point: an extension converts to its base, here to a const parameter
func Show(const APoint: Point);
begin
  println("({}, {})", APoint.X, APoint.Y);
end;

var
  GPoint: Point3;
  GFlat: Point;

begin
  GPoint.X := 1;
  GPoint.Y := 2;
  GPoint.Z := 3;
  Show(GPoint);
  // By value: the base part is copied
  GFlat := GPoint;
  println("{} {} {}", GFlat.X, GFlat.Y, GPoint.Z);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
(1, 2)
1 2 3

packed and packed(N) lay a record out as C's #pragma pack does, and an overlay holds its fields in one storage, as a C union does, zeroed wherever it is created.

Sets, pointers and static arrays

types_sets_pointers.adam
module exe types_sets_pointers;

type
  Color = choices(Red, Green, Blue);

  Colors = set of Color;

  Point = record
    X: int32;
    Y: int32;
  end;

var
  GMix: Colors;
  GSpot: Point;
  GWhere: ptr to Point;
  GFixed: array[0..2] of int32;

begin
  GMix := [Color.Red, Color.Blue];
  println("green in the mix: {}", Color.Green in GMix);
  GMix += [Color.Green];
  println("green now: {}", Color.Green in GMix);
  GWhere := addr of GSpot;
  GWhere->X := 4;
  GWhere^.Y := 9;
  println("{}, {}", GSpot.X, GSpot.Y);
  GFixed := [2, 3, 5];
  println("{} elements, the last {}", len(GFixed), GFixed[2]);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
green in the mix: false
green now: true
4, 9
3 elements, the last 5
  • set of a choices type, char, bool or a constant range holds at most 65536 members. +, - and * are union, difference and intersection, in tests a member, and >= / <= are superset and subset.
  • ptr to T points at a T: addr of X gives one, P^ reads and writes what it points to, and P->F or P^.F reach a field. ptr alone points at no type, and nil converts to every pointer.
  • array[lo..hi] of T is a static array with constant bounds, and an array literal such as [2, 3, 5] fills one where an array type is expected; a static array's count must match. A bracket literal whose place gives it no array or set type, such as a print argument, is an error (SM0773); for .. in and len type a bare one themselves.

Routine types

A routine type (func(const AN: int32): int32) is written wherever a type goes, and a routine named as a value converts to one only when the signatures agree. A closure, ref func(..), is on Closures.

↑ ↓ moveEnter openEsc close