Skip to content
Adam
Language

Objects

Objects carry fields and methods, interfaces name what they promise, and own, ref and weak hold them on the heap.

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

Methods and dispatch

An object type holds fields and methods, and a method reaches its object through self. object(Base) extends Base; a method is virtual only when it says virtual, and a kind that replaces it says override. ref T holds an object on the heap, and a ref to a derived object goes where a ref to its base is asked for; a virtual call through it reaches the object's own kind.

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

Inside a method parent.<member> names the inherited member itself, and parent.M() always runs the parent level's own M, so an override can run the method it replaces and add to it. parent alone, addr of parent or parent->X is an error (SM0016).

objects_parent.adam
module exe objects_parent;

type
  Greeter = object
    Name: string;

    virtual method Greet();
    begin
      println("hello, {}", self.Name);
    end;
  end;

  LoudGreeter = object(Greeter)
    // parent.Greet() always runs Greeter's own Greet, never this override again
    override method Greet();
    begin
      parent.Greet();
      println("HELLO, {}!", self.Name);
    end;
  end;

var
  GGreeter: ref LoudGreeter;

begin
  GGreeter := ref LoudGreeter;
  GGreeter.Name := "Adam";
  GGreeter.Greet();
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
hello, Adam
HELLO, Adam!

A const method changes nothing of its object, and only a const method can be called through a const object. A static method has no self and is called through its type's name.

Life: init and done

An object's init runs when it is built, and LBox: Box(3); gives it its arguments; its done runs when it ends. An object is built parent first, then its fields, then the init's body, and it ends in reverse.

objects_lifetime.adam
module exe objects_lifetime;

type
  Shape = interface
    const method Area(): int64;
  end;

  Box = object implements Shape
    hidden FSide: int64;

    property Side: int64 read FSide;

    init(const ASide: int64);
    begin
      self.FSide := ASide;
      println("box {} built", ASide);
    end;

    done;
    begin
      println("box {} ends", self.FSide);
    end;

    virtual const method Area(): int64;
    begin
      return self.FSide * self.FSide;
    end;
  end;

// The area of any shape
func Report(const AShape: Shape);
begin
  println("area {}", AShape.Area());
end;

// A box held by value: built at its declaration, ended when the routine ends
func ByValue();
var
  LBox: Box(3);
begin
  println("side {}", LBox.Side);
  Report(LBox);
end;

// A box on the heap: its own ends it with the routine
func OnTheHeap();
var
  LBox: own Box;
begin
  LBox := own Box(5);
  println("area {}", LBox.Area());
end;

begin
  ByValue();
  OnTheHeap();
  println("done");
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
box 3 built
side 3
area 9
box 3 ends
box 5 built
area 25
box 5 ends
done

hidden makes a member reachable only from the methods of its type and its descendants. A property is a value read and written through accessors: read F binds a field, read begin .. end computes the value, and a property with no write is read-only.

Interfaces

An interface is a set of methods with no body. object implements I promises each of them as a virtual method, and an object converts to every interface it implements. X is T tests an object's dynamic type and X as T is the checked cast (Objects).

Embeds

embed Life: Health; builds an object from other types (has-a). An embed is a field, built, copied and freed with its object as one, and its type is a record, an overlay or an object. A name the object does not declare is looked for in its embeds, the nearest depth first: GHero.HP is GHero.Life.HP. The embed can still be named itself, and must be where two embeds at the same depth have the name (SM0370). An embedded type's hidden members stay hidden, and an embed's method fulfils an interface the object implements, with no forwarding method written by hand.

objects_embed.adam
module exe objects_embed;

type
  Position = record
    X: int64;
    Y: int64;
  end;

  Health = object
    hidden FHit: int64;

    init();
    begin
      self.FHit := 10;
    end;

    property HP: int64 read FHit;

    method Damage(const AAmount: int64);
    begin
      self.FHit := self.FHit - AAmount;
      if self.FHit < 0 then
        self.FHit := 0;
      end;
    end;
  end;

  Player = object
    embed Pos: Position;
    embed Life: Health;
  end;

var
  GHero: Player;
  GCopy: Player;

begin
  // HP is Life.HP, X is Pos.X: the embeds' members, reached as the player's own
  GHero.X := 3;
  println("hp {} at {}", GHero.HP, GHero.X);
  GHero.Damage(4);
  // The copy's Health is its own
  GCopy := GHero;
  GCopy.Damage(9);
  println("hp {}, copy hp {}", GHero.HP, GCopy.HP);
  // The embed named itself
  println("pos.x {}", GHero.Pos.X);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
hp 10 at 3
hp 6, copy hp 0
pos.x 3

Owners

  • own T is the sole owner of a heap object: it moves, never copies.
  • ref T is a shared owner.
  • weak T watches a ref's object and is read through lock(W).

The object ends with its last owner.

Operators and extensions

A record or an object may declare operators (operator +(..)), and = / <> come as a pair. extend T .. end; adds methods and operators to a type declared elsewhere, another unit's included.

↑ ↓ moveEnter openEsc close