17: Interfaces - royal-lang/rl GitHub Wiki
An interface is a type that can be inherited by either structs or ref structs which forces them to implement specific functions or properties.
Interfaces can be used to create some kind of guarantees for the types.
A general name pattern for interfaces is to put a capitalized I before their type name.
Functions and properties in interfaces must not include a body.
interface NAME
{
}
Example:
interface IEntity
{
fn revive();
fn kill();
prop string EntityTypeName { get; }
}
ref struct Monster : IEntity
{
fn revive() { ... }
fn kill() { ... }
prop string EntityTypeName { get { return "Monster"; } }
}
ref struct Player : IEntity // Error: Player does not implement: fn IEntity.kill();
{
fn revive() { ... }
prop string EntityTypeName { get { return "Player"; } }
}
An interface can also be used as a type for variables and parameters which allows different types implementing the same interface to share functionality.
Type information isn't lost either which means they can be cast to their original type if needed too.
fn handleEntities(IEntity entity)
{
...
}