Interfaces are named collections of method signatures. In AngelScript, we can define interfaces similarly to classes, but without implementation.
Here’s a basic interface for geometric shapes:
For our example, we’ll implement this interface on rect and circle types:
To implement an interface in AngelScript, we just need to implement all the methods in the interface. Here we implement geometry on rect:
The implementation for circle:
If a variable has an interface type, then we can call methods that are in the named interface. Here’s a generic measure function taking advantage of this to work on any geometry:
The circle and rect classes both implement the geometry interface so we can use instances of these classes as arguments to measure:
When you run this program, it will output the area and perimeter of both the rectangle and the circle.
Note that AngelScript uses @ for handle (reference) parameters, which is similar to how interfaces are typically used in other languages.