Interfaces

An interface describes an object by the procedures (methods) that can be called on it, without saying how they are written. Two kinds of object with the same interface can be used by the same code, each with its own methods. This is also how COM objects from Windows libraries are called.

Interface ✅

Interface Name [Extends Base]
  Method[.type]([parameters])
  ...
EndInterface

A variable of an interface type holds the address of an object. The object's first 8 bytes point to its method table: the addresses of its procedures, one 8-byte slot per method, in the order the interface lists them (the base interface's first). Calling obj\Method(a, b) looks the procedure up in the table and calls it with the object itself as an extra first argument, usually named *this, followed by a and b.

x doesn't make objects or tables for you: a structure whose first field points to a table is an object, and any structure of procedure addresses (or memory filled with PokeI) is a table.

Interface Animal
  Name.s()
  Legs()
EndInterface

Structure AnimalTable
  name.i
  legs.i
EndStructure

Structure Dog
  *vt
  name$
EndStructure

Structure Bird
  *vt
EndStructure

Procedure.s DogName(*this.Dog)
  ProcedureReturn *this\name$
EndProcedure
Procedure DogLegs(*this.Dog)
  ProcedureReturn 4
EndProcedure
Procedure.s BirdName(*this.Bird)
  ProcedureReturn "a bird"
EndProcedure
Procedure BirdLegs(*this.Bird)
  ProcedureReturn 2
EndProcedure

Global dogTable.AnimalTable, birdTable.AnimalTable
dogTable\name = @DogName() : dogTable\legs = @DogLegs()
birdTable\name = @BirdName() : birdTable\legs = @BirdLegs()

Procedure Show(a.Animal)                 ; works for any Animal
  PrintN(a\Name() + " has " + Str(a\Legs()) + " legs")
EndProcedure

OpenConsole()
Define rex.Dog, tweety.Bird
rex\vt = @dogTable : rex\name$ = "Rex"
tweety\vt = @birdTable
Show(@rex)
Show(@tweety)

Output:

Rex has 4 legs
a bird has 2 legs

Extends adds methods after a base interface's, so an object of the extended interface can also be used where the base one is expected:

Interface Counter
  Get()
EndInterface

Interface Resettable Extends Counter
  Reset()
EndInterface

Structure CounterObj
  *vt
  n.i
EndStructure

Structure CounterTable
  get.i
  reset.i
EndStructure

Procedure CounterGet(*this.CounterObj)
  *this\n + 1
  ProcedureReturn *this\n
EndProcedure
Procedure CounterReset(*this.CounterObj)
  *this\n = 0
EndProcedure

Global table.CounterTable
table\get = @CounterGet() : table\reset = @CounterReset()

OpenConsole()
Define obj.CounterObj
obj\vt = @table
Define c.Resettable = @obj
c\Get() : c\Get()
Define plain.Counter = @obj              ; the same object as the base interface
PrintN(Str(plain\Get()))
c\Reset()
PrintN(Str(c\Get()))

Output:

3
1

A method table can also be written as data, with Data.i @Procedure(), one per method in order. ?Label then gives the table's address:

Interface Greeter
  Hello.s(name$)
EndInterface

Structure GreeterObj
  *vt
  greeting$
EndStructure

Procedure.s GreeterHello(*this.GreeterObj, name$)
  ProcedureReturn *this\greeting$ + ", " + name$
EndProcedure

DataSection
  GreeterTable:
  Data.i @GreeterHello()
EndDataSection

OpenConsole()
Define obj.GreeterObj
obj\vt = ?GreeterTable
obj\greeting$ = "Hi"
Define g.Greeter = @obj
PrintN(g\Hello("Ann"))

Output:

Hi, Ann

The arguments of a method call are checked against its declaration, as for a procedure. Defined(Name, #PB_Interface) tells whether an interface exists.

A pointer variable of an interface type (*g.Greeter, C style Greeter *g) holds an object's address as g.Greeter does, and its methods are called the same way.

x's classes build all of this (the interface, the structure, the method table) from one declaration, and their objects are interface objects like this one.